package pkg9.pkg8;
import com.sun.istack.internal.logging.Logger;
import java.util.logging.Level;
class MyThread implements Runnable{
public void run(){
for(int i = 0 ; i < 5 ; i++){
System.out.println(Thread.currentThread().getName() + "i = " + i);
//執行禮讓
if( i == 3){
System.out.println("讓");
Thread.currentThread().yield(); //執行緒禮讓
}
}
}
}
public class Main {
public static void main(String[] args) {
MyThread my = new MyThread(); //產生實體物件
Thread t1 = new Thread(new MyThread(),"執行緒-A"); //產生實體Thread物件
Thread t2 = new Thread(new MyThread(),"執行緒-B");
Thread t3 = new Thread(new MyThread(),"執行緒-C");
//設定執行的優先順序
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.NORM_PRIORITY);
t3.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
t3.start();
}
}
run:
執行緒-Ai = 0
執行緒-Ai = 1
執行緒-Ai = 2
執行緒-Ai = 3
讓
執行緒-Bi = 0
執行緒-Ci = 0
執行緒-Ci = 1
執行緒-Ci = 2
執行緒-Ci = 3
讓
執行緒-Bi = 1
執行緒-Bi = 2
執行緒-Bi = 3
讓
執行緒-Ai = 4
執行緒-Ci = 4
執行緒-Bi = 4
BUILD SUCCESSFUL (total time: 0 seconds)
留言列表