Reputation: 49
I'm new to Java Multithreading. Curious to know the state of idle thread incase of ThreadPoolExecutor. Is it in RUNNABLE/WAITING?
In case the idle threads are in RUNNABLE state, how are the new tasks attached to idle threads? AFAIK we assign a runnable/callable object to thread/pool. But my question is how does ThreadPoolExecutor assign queued runnable objects to idle thread??
Upvotes: 0
Views: 587
Reputation: 718826
- Curious to know the state of idle thread in case of ThreadPoolExecutor. Is it in RUNNABLE/WAITING?
It will be WAITING. It is waiting (in a Queue.take()
call) for a new task to appear on the work queue. In the current implementations this involves a mechanism similar to wait
/ notify
.
Your second question is therefore moot.
However, it is worth noting the following:
No "idle" thread will ever be RUNNABLE.
In current generation HotSpot JVMs, the actual scheduling (deciding which threads get priority and assigning them a core to run on) is handled by the operating system.
In Loom JVMs (Loom is still an Incubator project), light-weight virtual threads ("fibres") are scheduled (to a native thread) by the JVM rather than the OS.
Upvotes: 0
Reputation: 11421
It's easy enough to find out:
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.io.IOException;
public class ThreadExample {
public static void main(String[] args) throws IOException {
Executor executor = Executors.newFixedThreadPool(5);
// force the threads to be started
for (int i = 0; i < 5; i++) {
executor.execute(() -> {
try {Thread.sleep(1000);} catch (InterruptedException e) {
}
});
}
// don't terminate
System.in.read();
}
}
Run it:
$ javac ThreadExample.java
$ java ThreadExample
In another console, having waited at least one second for the tasks to complete:
$ ps
PID TTY TIME CMD
3640 ttys000 0:00.25 -bash
5792 ttys000 0:00.15 java ThreadExample
5842 ttys001 0:00.05 -bash
$ jstack 5792
...
"pool-1-thread-1" #12 prio=5 os_prio=31 cpu=1.77ms elapsed=13.37s tid=0x00007fe99f833800 nid=0xa203 waiting on condition [0x00007000094b2000]
java.lang.Thread.State: WAITING (parking)
at jdk.internal.misc.Unsafe.park([email protected]/Native Method)
- parking to wait for <0x000000061ff9e998> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park([email protected]/LockSupport.java:194)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await([email protected]/AbstractQueuedSynchronizer.java:2081)
at java.util.concurrent.LinkedBlockingQueue.take([email protected]/LinkedBlockingQueue.java:433)
at java.util.concurrent.ThreadPoolExecutor.getTask([email protected]/ThreadPoolExecutor.java:1054)
at java.util.concurrent.ThreadPoolExecutor.runWorker([email protected]/ThreadPoolExecutor.java:1114)
at java.util.concurrent.ThreadPoolExecutor$Worker.run([email protected]/ThreadPoolExecutor.java:628)
at java.lang.Thread.run([email protected]/Thread.java:834)
...
All the pool threads are in that state.
Upvotes: 1