Here's my following code:
ExecutorService executor = Executors.newFixedThreadPool(5);
executor.submit(new Runnable() {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("Start"+" "+Thread.currentThread().getName());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("End!"+" "+Thread.currentThread().getName());
}
}
});
executor.shutdown();
executor.awaitTermination(1, TimeUnit.SECONDS);
Here's my output:
Start pool-1-thread-1
End! pool-1-thread-1
Start pool-1-thread-1
End! pool-1-thread-1
Start pool-1-thread-1
End! pool-1-thread-1
Start pool-1-thread-1
End! pool-1-thread-1
Start pool-1-thread-1
End! pool-1-thread-1
My understanding is that in my code there're 5
threads in FixedThreadPool
. So when I run my code, it's supposed to output all 5
threads, right? Or Am I misunderstanding something?
In my output thread-1
starts and end everytime but isn't it supposed to output all 5
threads when it loops in for
loop?
Cause if only 1thread
is doing the task
then why do we even need 5 threads
?
Is there any way all 5
threads can output on the console?(I'm a newbie)