1717package io .opencensus .implcore .trace .export ;
1818
1919import com .google .common .annotations .VisibleForTesting ;
20+ import io .opencensus .implcore .internal .DaemonThreadFactory ;
2021import io .opencensus .implcore .trace .SpanImpl ;
2122import io .opencensus .trace .export .ExportComponent ;
2223import io .opencensus .trace .export .SpanData ;
3536public final class SpanExporterImpl extends SpanExporter {
3637 private static final Logger logger = Logger .getLogger (ExportComponent .class .getName ());
3738
38- private final WorkerThread workerThread ;
39+ private final Worker worker ;
40+ private final Thread workerThread ;
3941
4042 /**
4143 * Constructs a {@code SpanExporterImpl} that exports the {@link SpanData} asynchronously.
@@ -49,9 +51,8 @@ public final class SpanExporterImpl extends SpanExporter {
4951 */
5052 static SpanExporterImpl create (int bufferSize , long scheduleDelayMillis ) {
5153 // TODO(bdrutu): Consider to add a shutdown hook to not avoid dropping data.
52- WorkerThread workerThread = new WorkerThread (bufferSize , scheduleDelayMillis );
53- workerThread .start ();
54- return new SpanExporterImpl (workerThread );
54+ Worker worker = new Worker (bufferSize , scheduleDelayMillis );
55+ return new SpanExporterImpl (worker );
5556 }
5657
5758 /**
@@ -60,29 +61,32 @@ static SpanExporterImpl create(int bufferSize, long scheduleDelayMillis) {
6061 * @param span the {@code Span} to be added.
6162 */
6263 public void addSpan (SpanImpl span ) {
63- workerThread .addSpan (span );
64+ worker .addSpan (span );
6465 }
6566
6667 @ Override
6768 public void registerHandler (String name , Handler handler ) {
68- workerThread .registerHandler (name , handler );
69+ worker .registerHandler (name , handler );
6970 }
7071
7172 @ Override
7273 public void unregisterHandler (String name ) {
73- workerThread .unregisterHandler (name );
74+ worker .unregisterHandler (name );
7475 }
7576
76- private SpanExporterImpl (WorkerThread workerThread ) {
77- this .workerThread = workerThread ;
77+ private SpanExporterImpl (Worker worker ) {
78+ this .workerThread =
79+ new DaemonThreadFactory ("ExportComponent.ServiceExporterThread" ).newThread (worker );
80+ this .workerThread .start ();
81+ this .worker = worker ;
7882 }
7983
8084 @ VisibleForTesting
8185 Thread getServiceExporterThread () {
8286 return workerThread ;
8387 }
8488
85- // Worker thread that batches multiple span data and calls the registered services to export
89+ // Worker in a thread that batches multiple span data and calls the registered services to export
8690 // that data.
8791 //
8892 // The map of registered handlers is implemented using ConcurrentHashMap ensuring full
@@ -91,7 +95,7 @@ Thread getServiceExporterThread() {
9195 //
9296 // The list of batched data is protected by an explicit monitor object which ensures full
9397 // concurrency.
94- private static final class WorkerThread extends Thread {
98+ private static final class Worker implements Runnable {
9599 private final Object monitor = new Object ();
96100
97101 @ GuardedBy ("monitor" )
@@ -140,12 +144,10 @@ private void onBatchExport(List<SpanData> spanDataList) {
140144
141145 // TODO: Decide whether to use a different class instead of LinkedList.
142146 @ SuppressWarnings ("JdkObsolete" )
143- private WorkerThread (int bufferSize , long scheduleDelayMillis ) {
147+ private Worker (int bufferSize , long scheduleDelayMillis ) {
144148 spans = new LinkedList <SpanImpl >();
145149 this .bufferSize = bufferSize ;
146150 this .scheduleDelayMillis = scheduleDelayMillis ;
147- setDaemon (true );
148- setName ("ExportComponent.ServiceExporterThread" );
149151 }
150152
151153 // Returns an unmodifiable list of all buffered spans data to ensure that any registered
0 commit comments