diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /jdisc_core/src/main/java/com/yahoo/jdisc/application/ContainerThread.java |
Publish
Diffstat (limited to 'jdisc_core/src/main/java/com/yahoo/jdisc/application/ContainerThread.java')
-rw-r--r-- | jdisc_core/src/main/java/com/yahoo/jdisc/application/ContainerThread.java | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/application/ContainerThread.java b/jdisc_core/src/main/java/com/yahoo/jdisc/application/ContainerThread.java new file mode 100644 index 00000000000..38527acc099 --- /dev/null +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/application/ContainerThread.java @@ -0,0 +1,60 @@ +// 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 <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> + */ +public class ContainerThread extends Thread { + + private final MetricConsumer consumer; + + /** + * <p>Allocates a new ContainerThread object. This constructor calls the parent {@link Thread#Thread(Runnable)} + * constructor.</p> + * + * @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; + } + + /** + * <p>This class implements the {@link ThreadFactory} interface on top of a {@link Provider} for {@link + * MetricConsumer} instances.</p> + */ + 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()); + } + } +} |