How to avoid busy spinning in Java

前端 未结 4 2170
南旧
南旧 2021-02-09 16:02

I have a multi-threaded application where a thread sends a message to another thread. The waiting thread polls for the message and reacts (locks are handled). Like this:

4条回答
  •  长发绾君心
    2021-02-09 16:25

    Have you tried to put some Thread.sleep to avoid executing the while loop constantly? It will liberate CPU for your other threads and avoid hogs

    http://docs.oracle.com/javase/tutorial/essential/concurrency/sleep.html

提交回复
热议问题