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.