thread 基础

来源:百度知道 编辑:UC知道 时间:2024/05/05 01:49:03
public class duocheng extends Thread
{ Thread th1=new Thread(this,"1");
Thread th2=new Thread(this,"2");
Thread th3=new Thread(this,"3");

public duocheng()
{
th1.setPriority(4);
th2.setPriority(6);
th3.setPriority(7);
th1.start();
th2.start();
th3.start();
}
public void run()
{
for(int i=0;i<3;i++)
System.out.println(getPriority());
}
public static void main(String args[])
{new duocheng();

}
}
为什么输出的全是5,而不是我设置的4,6,7

你这里写错了 System.out.println(getPriority());
这里相当于是 Thread.getPriority() 当然这里 Thread 默认的优先级是5
如果你想输出的定义的优先级 你定义的线程
Thread th1=new Thread(this,"1");
然后你设置 th1的优先级th1.setPriority(4);
你获取th1的优先级的话 应该这样写
th1.getPriority() 这样才可以