views:

330

answers:

4

I am working on a mutual exclusion assignment, but when I started I noticed my application's thread ID's start at 9. It doesn't change when I compile and execute it again. Is there some problem I'm missing, or can Java Thread IDs start at an arbitrary number? This question is related.


For those interested, here is a class from Herlihy & Shavit's "The Art of Multiprocessor Programming" for numbering threads:

public class ThreadID {
    private static volatile int nextID = 0;
    private static class ThreadLocalID extends ThreadLocal<Integer> {
     protected synchronized Integer initialValue() {
      return nextID++;
     }
    }

    private static ThreadLocalID threadID = new ThreadLocalID();
    public static int get() {
     return threadID.get();
    }
    public static void set(int index) {
     threadID.set(index);
    }
}

You can then call

ThreadID.get();

which will automatically increment numbers and always start at 1.

+7  A: 
John Calsbeek
A: 

I don't know if there's a spec that defines this, or how reliable or consistent this numbering is going to be, but the threads you create in your code are not the first running threads in the system, a number of threads start up before your code runs as part of the JVM. Here's what jstack says is running on my jvm when I run your code (with a sleep in there so I can measure):

"Attach Listener" daemon prio=10 tid=0x000000004038c400 nid=0x3adf runnable [0x0000000000000000..0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Low Memory Detector" daemon prio=10 tid=0x00007f7dc4002400 nid=0x3ac5 runnable [0x0000000000000000..0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"CompilerThread1" daemon prio=10 tid=0x0000000040386400 nid=0x3ac4 waiting on condition [0x0000000000000000..0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"CompilerThread0" daemon prio=10 tid=0x0000000040384000 nid=0x3ac3 waiting on condition [0x0000000000000000..0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" daemon prio=10 tid=0x0000000040382000 nid=0x3ac2 runnable [0x0000000000000000..0x0000000040c43710]
   java.lang.Thread.State: RUNNABLE

"Finalizer" daemon prio=10 tid=0x0000000040363000 nid=0x3ac1 in Object.wait() [0x0000000042186000..0x0000000042186a00]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00007f7dfaaa1210> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:116)
    - locked <0x00007f7dfaaa1210> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:132)
    at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:159)

"Reference Handler" daemon prio=10 tid=0x000000004035bc00 nid=0x3ac0 in Object.wait() [0x0000000042085000..0x0000000042085d80]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00007f7dfaaa1078> (a java.lang.ref.Reference$Lock)
    at java.lang.Object.wait(Object.java:485)
    at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:116)
    - locked <0x00007f7dfaaa1078> (a java.lang.ref.Reference$Lock)

"main" prio=10 tid=0x00000000402f5800 nid=0x3abc waiting on condition [0x0000000041015000..0x0000000041015ec0]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at Simulation.main(Simulation.java:16)

"VM Thread" prio=10 tid=0x0000000040356400 nid=0x3abf runnable 

"GC task thread#0 (ParallelGC)" prio=10 tid=0x0000000040300400 nid=0x3abd runnable 

"GC task thread#1 (ParallelGC)" prio=10 tid=0x0000000040301c00 nid=0x3abe runnable 

"VM Periodic Task Thread" prio=10 tid=0x00007f7dc4004c00 nid=0x3ac6 waiting on condition
Steve B.
+1  A: 

Yes. But the thread ID is shared by whole JVM so for you application it could start from any number.

ZZ Coder
A: 

The thread ID is a positive long number generated when this thread was created. The thread ID is unique and remains unchanged during its lifetime. When a thread is terminated, this thread ID may be reused.

reference: http://java.sun.com/javase/6/docs/api/java/lang/Thread.html

It doesn't need to start from 0 or any other number.

Firstthumb