Reputation: 19225
IN my app I use a threadpool with a custom ThreadFactory.
My code looks like:
pool = Executors.newScheduledThreadPool(10, new TF());
class TF implements ThreadFactory {
AtomicInteger count = new AtomicInteger(1);
public synchronized Thread newThread(Runnable r) {
Thread t = new Thread(r) ;
t.setName("ThreadPool Thread[" + count.getAndIncrement() + "]");
t.setUncaughtExceptionHandler(new UEHLogger());
return t;
}
}
However, after submitting various Runnables to the threadpool, if I dump the current threads (from the IDE, Intellij IDEA), I get:
"ThreadPool Thread[1]" daemon prio=6 tid=0x0334e000 nid=0x1130 waiting on condition [0x0377f000..0x0377fc94]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x22fa7838> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:158)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:1925)
at java.util.concurrent.DelayQueue.take(DelayQueue.java:160)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:583)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:576)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:947)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)
"ThreadPool Thread[1]" daemon prio=6 tid=0x0333e400 nid=0x128 waiting on condition [0x0372f000..0x0372fd14]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x22edb9e0> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:198)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:1963)
at java.util.concurrent.DelayQueue.take(DelayQueue.java:164)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:583)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:576)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:947)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)
(As well as simliar stack traces for threads 2-9 inclusive)
So basically instead of getting threads number 1,2,3,4,5,6,7,8,9,10 i get threads numbered 1,1,2,3,4,5,6,7,8,9
Everything seems to be working OK, but its obviously disconcerting.
Upvotes: 4
Views: 6776
Reputation: 284796
Hmm, can't reproduce the problem with OpenJDK and the following (highly simplified) test code. What does the below give for you?
class TF implements ThreadFactory {
class UEHLogger implements Thread.UncaughtExceptionHandler
{
public void uncaughtException(Thread t, Throwable e) {
System.out.println(t + " threw exception: " + e);
}
}
AtomicInteger count = new AtomicInteger(1);
public synchronized Thread newThread(Runnable r) {
Thread t = new Thread(r) ;
t.setName("ThreadPool Thread[" + count.getAndIncrement() + "]");
t.setUncaughtExceptionHandler(new UEHLogger());
return t;
}
public static void main(String[] a)
{
TF myTF = new TF();
Thread[] threads = new Thread[10];
for(int i = 0; i < threads.length; i++)
threads[i] = myTF.newThread(new Runnable(){public void run(){}});
for(Thread t : threads)
System.out.println(t);
}
}
Upvotes: 1
Reputation: 272267
You haven't inadvertently created two threadpools (or two ThreadFactories), have you ?
It may be an idea to confirm this by getting each thread to output the id of its thread factory alongside its own id.
Upvotes: 5