...
- Each thread provides a thread id that is unique within one JVM
- Threads can only be started once (a second start will result in an exception)
- Thread pools
- Application server usually make use of thread pools, that is they re-use threads and just provide new Runnable instances to thread
- Thus the thread ID is not uniquely identifying a job that is executed by a thread, but one thread ID will in fact run multiple jobs
- The new Executor classes in Java6 provide an implementation of a thread pool
- Runnables
- Are used to execute jobs (in fact each Thread defines its functionality as Runnable)
- Runnables can be reused
Thread id + instrumented Runnable run-counter
One possibility to uniquely identify a "job that is run within another thread" even if thread pools are used is to instrument each Runnable run method with an additional counter that is increased every time the run method is started. So even if the Runnable is re-used a new identification will be created. In addition to that we could use the name of the Runnable realization class to differentiate between multiple Runnables realizations. To make this combination unique over multiple threads, we could integrate the thread-id.