Java - join Threads created in switch case | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

6th Jun 2018, 6:45 AM
Jakub Grucel
Jakub Grucel - avatar
1 Answer
+ 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.
6th Jun 2018, 8:21 AM
Tashi N
Tashi N - avatar