java 多线程 优先级_JAVA多线程优先级运行顺序的问题

先上代码classmyRunnable1implementsRunnable{publicvoidrun(){for(inti=0;i<50000;i++){System.out.print("1");}}}classmyRunnable2implementsRunnable{publicvoidrun(){for(inti=0;...

先上代码

class myRunnable1 implements Runnable

{

public void run()

{

for(int i=0;i<50000;i++)

{

System.out.print("1");

}

}

}

class myRunnable2 implements Runnable

{

public void run()

{

for(int i=0;i<50000;i++)

{

System.out.print("2");

}

}

}

class myRunnable3 implements Runnable

{

public void run()

{

for(int i=0;i<50000;i++)

{

System.out.print("3");

}

}

}

class myRunnable4 implements Runnable

{

public void run()

{

for(int i=0;i<50000;i++)

{

System.out.print("4");

}

}

}

class myRunnable5 implements Runnable

{

public void run()

{

for(int i=0;i<50000;i++)

{

System.out.print("5");

}

}

}

class myRunnable6 implements Runnable

{

public void run()

{

for(int i=0;i<50000;i++)

{

System.out.print("6");

}

}

}

public class myRun

{

public static void main(String[] args)

{

myRunnable1 mr1=new myRunnable1();

myRunnable2 mr2=new myRunnable2();

myRunnable3 mr3=new myRunnable3();

myRunnable4 mr4=new myRunnable4();

myRunnable5 mr5=new myRunnable5();

myRunnable6 mr6=new myRunnable6();

Thread t1=new Thread(mr1);

Thread t2=new Thread(mr2);

Thread t3=new Thread(mr3);

Thread t4=new Thread(mr4);

Thread t5=new Thread(mr5);

Thread t6=new Thread(mr6);

t1.setPriority(Thread.MAX_PRIORITY);

t2.setPriority(Thread.MAX_PRIORITY);

t3.setPriority(Thread.MAX_PRIORITY);

t4.setPriority(Thread.MAX_PRIORITY);

t5.setPriority(Thread.MAX_PRIORITY);

t6.setPriority(Thread.MIN_PRIORITY);

t1.start();

t2.start();

t3.start();

t4.start();

t5.start();

t6.start();

}

}

设置了优先级,6个线程运行的顺序是随机的。最开始我只写了2个线程,设置优先级,运行随机,我以为是4核CPU可以同时运行,所以不分优先级。后来我写了6个线程,也把运行次数提升到了50000次,前5个线程最高优先级,第六个最低优先级,但运行顺序还是随机的。求各位高手解释啊。。。

展开

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值