Setting priority to Java's threads

前端 未结 5 2071
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 20:14

I have a program that runs in a few threads. The main thread shares an object with the other threads and in the main I have a call to:

synchronized(obj){
            


        
5条回答
  •  -上瘾入骨i
    2020-11-27 20:36

    Increasing the main thread's priority the way Macarse says will probably work. However, you are relying on the platform's preemptive thread scheduler to work properly. You should instead call the Thread.yield() static method in your worker threads when they are done with whatever important sections of code they are running. This is a good habit to get into even when you are using different levels of thread priority.

提交回复
热议问题