running 3 threads in sequence java

前端 未结 10 622
天涯浪人
天涯浪人 2020-11-29 04:35

I have 3 threads 1st printing A 2nd printing B 3rd printing C

I want to print in sequence A B C A B C A B C and so on.....

So I wrote the program below, but

10条回答
  •  我在风中等你
    2020-11-29 05:17

    Convert those IF statements to WHILE statements to get the desired behavior:

    if (notifyAllExample.status != 2){
        notifyAllExample.wait();
    }
    

    to

    while (notifyAllExample.status != 2){
        notifyAllExample.wait();
    }
    

    This will ensure that if a thread is notified, it won't go out of the while loop until the status value is what it expects.

    Also, mark status as volatile so that the threads won't have a local copy.

提交回复
热议问题