Setting priority to Java's threads

前端 未结 5 2070
没有蜡笔的小新
没有蜡笔的小新 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条回答
  •  無奈伤痛
    2020-11-27 20:45

    you can use the setPriority() method. For example:

    new MyThread("Foo").start(); 
    Thread bar = new MyThread("Bar"); 
    bar.setPriority( Thread.NORM_PRIORITY + 1 ); 
    bar.start();
    

    This gives bar the new priority which should quickly take over Foo

    Edit:

    To answer your comment, you can set the max priortiy using:

    bar.setPriority( Thread.MAX_PRIORITY );
    

提交回复
热议问题