源代码
public class Test {
public static void main(String[] args){
MyThread threadObj1 = new MyThread("first thread");
threadObj1.setPriority(Thread.MIN_PRIORITY);
threadObj1.start();
MyThread threadObj2 = new MyThread("second thread");
threadObj2.start();
MyThread threadObj3 = new MyThread("third thread");
threadObj3.setPriority(Thread.MAX_PRIORITY);
threadObj3.start();
MyThread threadObj4 = new MyThread("forth thread");
threadObj4.setPriority(Thread.MAX_PRIORITY);
threadObj4.start();
}
}
class MyThread extends Thread{
String name;
int nsteps = 5;
MyThread(String name){
// super(name);
this.name = name;
}
public void run(){
System.out.println(name+"begains running");
for(int i=0;i<nsteps;i++){
System.out.println("step"+i+":"+name+"is running with priority "+getPriority());
}
System.out.println(name+" is finished!");
}
}
怎么我运行的时候,低线程的还比高线程的先finished也?不明白,希望有高手来给小生解惑···
public class Test {
public static void main(String[] args){
MyThread threadObj1 = new MyThread("first thread");
threadObj1.setPriority(Thread.MIN_PRIORITY);
threadObj1.start();
MyThread threadObj2 = new MyThread("second thread");
threadObj2.start();
MyThread threadObj3 = new MyThread("third thread");
threadObj3.setPriority(Thread.MAX_PRIORITY);
threadObj3.start();
MyThread threadObj4 = new MyThread("forth thread");
threadObj4.setPriority(Thread.MAX_PRIORITY);
threadObj4.start();
}
}
class MyThread extends Thread{
String name;
int nsteps = 5;
MyThread(String name){
// super(name);
this.name = name;
}
public void run(){
System.out.println(name+"begains running");
for(int i=0;i<nsteps;i++){
System.out.println("step"+i+":"+name+"is running with priority "+getPriority());
}
System.out.println(name+" is finished!");
}
}
怎么我运行的时候,低线程的还比高线程的先finished也?不明白,希望有高手来给小生解惑···
我也是正研究这。。
我认为,这几个线程是交互运行,只是优先级高的会先执行,但是不是优先级高的执行完后优先级 低的才执行。会给优先级低的线程执行一小会儿nsteps只有5.高优先级的让低的执行那么一小会儿就结束了。。你试试把nsteps设成100.