One question, if 10 threads are opened, but after all 10 threads are executed, they are handed over to the main thread for unified input. How to achieve this? Below I paste the code:
int count = 10;
List<Thread> workers = new ArrayList<>();
for(int i = 0; i < count; i++) {
Thread worker = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("执行子线程");
}
});
worker.start();
workers.add(worker);
}
for(int i = 0; i < count; i++) {
workers.get(i).join();
}
System.out.println("执行主线程");
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
After all threads start, wait for the thread execution to complete through the join method, and then execute the following main thread method when all threads are completed.