Java 執行緒的優先順序是使用 1~10來表示,數字大代表優先權較高(意指先執行)。
multi_Test apple=new multi_Test("red");
multi_Test banana=new multi_Test("yellow");
multi_Test grape=new multi_Test("purple");
multi_Test cloud =new multi_Test("white");
multi_Test grass=new multi_Test("green");
apple.setPriority(Thread.MAX_PRIORITY);
banana.setPriority(Thread.MIN_PRIORITY);
cloud.setPriority(3);
grass.setPriority(6);
class multi_Test
{
private String name;
public multi_Test(String str)
{
name=str;
}
public void show()
{
for(int i=0;i<3;i++)
{
try
{
sleep((int)(1000*Math.random()));
}
catch(InterruptedException e){}
System.out.println(name+" 執行中"+this.getPriority());
}
}
}
public class multi_thread_3
{
public static void main(String args[])
{
multi_Test apple=new multi_Test("red");
multi_Test banana=new multi_Test("yellow");
multi_Test grape=new multi_Test("purple");
multi_Test cloud =new multi_Test("white");
multi_Test grass=new multi_Test("green");
apple.setPriority(Thread.MAX_PRIORITY);
banana.setPriority(Thread.MIN_PRIORITY);
cloud.setPriority(3);
grass.setPriority(6);
apple.start();
banana.start();
grape.start();
cloud.start();
grass.start();
}
}