0
Java - join Threads created in switch case
Hello, my Programm start threads in a switchcase and the threads are adding nodes to a linked list. Code: case "1":Worker w1= new Worker(line, headArray); w1.start(); w1.join(); break; case "2":Worker w2= new Worker(line, patArray); w2.start(); w2.join(); break; I want to work further with the changed list and have to wait untill all threads are done but as you can see the programm waits till one thread ist done before it creates the second. Do Somebody know a better solution?
1 Odpowiedź
+ 8
This is not a good solution.
The threads only exist locally in the switch case statement.
You'll try to join one thread before the other exists whatever case you invoke first.
First of all you need to synchronize the list changes which both threads will execute.
Then initialize the threads outside the conditional statement and check if they're running before calling the join method.