aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/main/java/com/yahoo/jdisc/application/ContainerThread.java
blob: 6daf7a0c94531c313d82d4d0ce0a32706b662332 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.application;

import com.google.inject.Inject;
import com.google.inject.Provider;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * <p>This class decorates {@link Thread} to allow for internal jDISC optimizations. Whenever possible a jDISC
 * application should use this class instead of Thread. The {@link ContainerThread.Factory} class is a helper-class for
 * working with the {@link Executors} framework.</p>
 *
 * @author Simon Thoresen Hult
 */
public class ContainerThread extends Thread {

    private final MetricConsumer consumer;

    /**
     * Allocates a new ContainerThread object. This constructor calls the parent {@link Thread#Thread(Runnable)}
     * constructor.
     *
     * @param target   the object whose <code>run</code> method is called.
     * @param consumer the MetricConsumer of this thread.
     */
    public ContainerThread(Runnable target, MetricConsumer consumer) {
        super(target);
        this.consumer = consumer;
    }

    /**
     * <p>Returns the {@link MetricConsumer} of this. Note that this may be null.</p>
     *
     * @return The MetricConsumer of this, or null.
     */
    public MetricConsumer consumer() {
        return consumer;
    }

    /**
     * This class implements the {@link ThreadFactory} interface on top of a {@link Provider} for {@link
     * MetricConsumer} instances.
     */
    public static class Factory implements ThreadFactory {

        private final Provider<MetricConsumer> provider;

        @Inject
        public Factory(Provider<MetricConsumer> provider) {
            this.provider = provider;
        }

        @Override
        public Thread newThread(Runnable target) {
            return new ContainerThread(target, provider.get());
        }

    }

}