先上代码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个线程最高优先级,第六个最低优先级,但运行顺序还是随机的。求各位高手解释啊。。。
展开