IllegalMonitorStateException on wait() call

后端 未结 10 1509
感动是毒
感动是毒 2020-11-22 11:37

I am using multi-threading in java for my program. I have run thread successfully but when I am using Thread.wait(), it is throwing java.lang.IllegalMonit

10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 12:05

    You need to be in a synchronized block in order for Object.wait() to work.

    Also, I recommend looking at the concurrency packages instead of the old school threading packages. They are safer and way easier to work with.

    Happy coding.

    EDIT

    I assumed you meant Object.wait() as your exception is what happens when you try to gain access without holding the objects lock.

提交回复
热议问题