Answer the question
In order to leave comments, you need to log in
How is the order of text output determined in a multithreaded program?
Hello!
I'm currently learning multithreading in Java and I have a small question.
Let's say we have a program like this:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
class Task implements Runnable {
private int countdown = 5;
@Override
public void run() {
while (countdown-- > 0)
System.out.println(Thread.currentThread().getName() + "- (" + countdown + ")");
}
}
public class Program {
public static void main(String[] args) {
ExecutorService exc = Executors.newFixedThreadPool(5);
for (int i = 0; i < 5; i++) {
exc.execute(new Task());
}
exc.shutdown();
}
}
pool-1-thread-1- (4)
pool-1-thread-1- (3)
pool-1-thread-1- (2)
pool-1-thread-1- (1)
pool-1-thread-1- (0)
pool-1-thread-2- (4)
pool-1-thread-2- (3)
pool-1-thread-2- (2)
pool-1-thread-2- (1)
pool-1-thread-2- (0)
pool-1-thread-3- (4)
pool-1-thread-3- (3)
pool-1-thread-3- (2)
pool-1-thread-3- (1)
pool-1-thread-3- (0)
pool-1-thread-5- (4)
pool-1-thread-5- (3)
pool-1-thread-5- (2)
pool-1-thread-5- (1)
pool-1-thread-5- (0)
pool-1-thread-4- (4)
pool-1-thread-4- (3)
pool-1-thread-4- (2)
pool-1-thread-4- (1)
pool-1-thread-4- (0)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question