Using “notify()” & “wait()” instead of “suspend()” and “resume()” to control a thread

前端 未结 3 880
梦毁少年i
梦毁少年i 2021-01-06 16:29

I\'m trying to learn how to pause and resume a thread in java. I\'m using an Applet that implements Runnablehas 2 buttons \"Start\" and \"Stop\".

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-06 17:29

    I think you have to synchronize on the thread in order to call wait and notify. Try to use

    synchronized (th) {
        th.notify();
    }
    

    and the same with wait().

提交回复
热议问题