Skip to content
Advertisement

synchronize two threads in java

I have two threads in my java programme, one is main thread and other thread is thread A which is spawned in main thread. now i want main thread to start thread A and wait till thread A has executed some part of its code in run method and thread A should suspend itself. main thread should then start running, run few lines of code and then again thread A should start from where it has stopped and vice versa. this should happen for n number of times. I am trying as belows:

Thread A class:

JavaScript

MainThread:

JavaScript

Expected output:

JavaScript

Actual output:

JavaScript

I don’t know why the whole for loop is getting executed in Thread A even when i’ve issued a notify() signal in job method.

Advertisement

Answer

Here is more simplified way

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement