JAVA_Join_Yield_Priority

 1 public class TestJoin {
 2   public static void main(String[] args) {
 3     MyThread2 t1 = new MyThread2("abcde");
 4     t1.start();
 5     try {
 6         t1.join();
 7     } catch (InterruptedException e) {}
 8
 9     for(int i=1;i<=10;i++){
10       System.out.println("i am main thread");
11     }
12   }
13 }
14 class MyThread2 extends Thread {
15   MyThread2(String s){
16       super(s);
17   }
18
19   public void run(){
20     for(int i =1;i<=10;i++){
21       System.out.println("i am "+getName());
22       try {
23           sleep(1000);
24       } catch (InterruptedException e) {
25           return;
26       }
27     }
28   }
29 }
 1 public class TestYield {
 2   public static void main(String[] args) {
 3     MyThread3 t1 = new MyThread3("t1");
 4     MyThread3 t2 = new MyThread3("t2");
 5     t1.start(); t2.start();
 6   }
 7 }
 8 class MyThread3 extends Thread {
 9   MyThread3(String s){super(s);}
10   public void run(){
11     for(int i =1;i<=100;i++){
12       System.out.println(getName()+": "+i);
13       if(i%10==0){
14         yield();
15       }
16     }
17   }
18 }

 1 public class TestPriority {
 2     public static void main(String[] args) {
 3         Thread t1 = new Thread(new T1());
 4         Thread t2 = new Thread(new T2());
 5         t1.setPriority(Thread.NORM_PRIORITY + 3);
 6         t1.start();
 7         t2.start();
 8     }
 9 }
10
11 class T1 implements Runnable {
12     public void run() {
13         for(int i=0; i<1000; i++) {
14             System.out.println("T1: " + i);
15         }
16     }
17 }
18
19 class T2 implements Runnable {
20     public void run() {
21         for(int i=0; i<1000; i++) {
22             System.out.println("------T2: " + i);
23         }
24     }
25 }
时间: 2024-10-13 21:31:05

JAVA_Join_Yield_Priority的相关文章