我想问一下这个多线程中.优先级高的线程是不是一定比优先级低的线程先运行呢
我刚在main里面实例两个线程 一个把优先级设置到9,一个是默认
结果发现优先级高的还没循环完毕.而优先级低的就在运行了得到了w:320 w:321 m:8 w:322这样的
那优先级是不是只是让优先级高的获得更多的资源只是一个概率问题而不是绝对先运行优先级高的呢?
代码:
private int count;
public void run(){
for(int i=0;i<1000;i++)
{
count=count+1;
System.out.print(Thread.currentThread().getName()+":"+count);
}
try {
sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}public static void main(String[] args) {
thred th=new thred();
th.setName("w");
th.setPriority(9);
thred t=new thred();
t.setName("m");
th.start();
t.start();
}
我刚在main里面实例两个线程 一个把优先级设置到9,一个是默认
结果发现优先级高的还没循环完毕.而优先级低的就在运行了得到了w:320 w:321 m:8 w:322这样的
那优先级是不是只是让优先级高的获得更多的资源只是一个概率问题而不是绝对先运行优先级高的呢?
代码:
private int count;
public void run(){
for(int i=0;i<1000;i++)
{
count=count+1;
System.out.print(Thread.currentThread().getName()+":"+count);
}
try {
sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}public static void main(String[] args) {
thred th=new thred();
th.setName("w");
th.setPriority(9);
thred t=new thred();
t.setName("m");
th.start();
t.start();
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货