Notify not getting the thread out of wait state

前端 未结 3 1182
盖世英雄少女心
盖世英雄少女心 2021-01-28 05:28

I am trying to use 2 threads. 1 thread prints only odd number and the other thread prints only even number and It has to be an alternative operation.

Eg:



        
3条回答
  •  野的像风
    2021-01-28 06:08

    You have a real confusion of threads and locks. I suggest you create one and only one object to use for locking to start with as you don't appear to have a clear idea what you are locking.

    If you notify() and nothing is listening, the signal is lost. However, a wait() can wake spuriously.

    For this reason, a notify() should be accompanied by a state change and a wait() should be in a loop checking that change.

提交回复
热议问题