blob: 7ceb3a1e765f0f282d0fcf0d74cb807a66d9b247 (
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
|
// Copyright 2016 Yahoo Inc. 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
*/
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());
}
}
}
|