標籤:
join()方法樣本如下:
1 public class Thread1{ 2 public static void main(String[] args) { 3 TestThread t=new TestThread("t"); 4 t.start(); 5 try{ 6 t.join(); //類似於調用run()方法. 7 }catch (InterruptedException e) { 8 9 }10 for (int i=0;i<=10 ;i++ ) {11 System.out.println("i am main Thread!!");12 }13 }14 }15 16 class TestThread extends Thread{17 public TestThread(String s){18 super(s);19 }20 public void run(){21 for (int i=0;i<=10;i++) {22 System.out.println(" I‘m "+getName()+" Thread!!");23 try{24 sleep(1000); // sleep()必須catch Exception.25 }catch (InterruptedException e) {26 return;27 }28 }29 }30 }
輸出為:
1 i‘m t Thread!! 2 i‘m t Thread!! 3 i‘m t Thread!! 4 i‘m t Thread!! 5 i‘m t Thread!! 6 i‘m t Thread!! 7 i‘m t Thread!! 8 i‘m t Thread!! 9 i‘m t Thread!!10 i‘m t Thread!!11 i‘m main Thread!!12 i‘m main Thread!!13 i‘m main Thread!!14 i‘m main Thread!!15 i‘m main Thread!!16 i‘m main Thread!!17 i‘m main Thread!!18 i‘m main Thread!!19 i‘m main Thread!!20 i‘m main Thread!!
yield()方法樣本:
1 public class Thread1{ 2 public static void main(String[] args) { 3 TestThread t1=new TestThread("t1"); 4 TestThread t2=new TestThread("t2"); 5 t1.start(); t2.start(); 6 } 7 } 8 9 class TestThread extends Thread{10 public TestThread(String s){11 super(s);12 }13 public void run(){14 for (int i=1;i<=100;i++) {15 System.out.println(getName()+":"+i);16 if(i%10==0){17 yield(); //當i%10==0時.會讓出CPU.18 } 19 }20 }21 }
輸出:每當i%10==0時,當前線程t1(t2)必定會讓出CPU給t2(t1).
1 t1:1 2 t1:2 3 t1:3 4 t1:4 5 t1:5 6 t1:6 7 t1:7 8 t2:1 9 t1:8 10 t2:2 11 t1:9 12 t2:3 13 t1:10 14 t2:4 15 t1:11 16 t2:5 17 t2:6 18 t1:12 19 t1:13 20 t2:7 21 t2:8 22 t2:9 23 t2:10 24 t1:14 25 t1:15 26 t1:16 27 t1:17 28 t1:18 29 t1:19 30 t1:20 31 t2:11 32 t2:12 33 t2:13 34 t2:14 35 t2:15 36 t2:16 37 t2:17 38 t2:18 39 t2:19 40 t2:20 41 t1:21 42 t1:22 43 t1:23 44 t1:24 45 t1:25 46 t1:26 47 t1:27 48 t1:28 49 t1:29 50 t1:30 51 t2:21 52 t2:22 53 t2:23 54 t2:24 55 t2:25 56 t2:26 57 t2:27 58 t2:28 59 t2:29 60 t2:30 61 t2:31 62 t2:32 63 t2:33 64 t2:34 65 t2:35 66 t2:36 67 t2:37 68 t2:38 69 t2:39 70 t2:40 71 t2:41 72 t2:42 73 t2:43 74 t2:44 75 t2:45 76 t2:46 77 t2:47 78 t2:48 79 t2:49 80 t2:50 81 t1:31 82 t1:32 83 t1:33 84 t1:34 85 t1:35 86 t1:36 87 t1:37 88 t1:38 89 t1:39 90 t1:40 91 t2:51 92 t2:52 93 t2:53 94 t2:54 95 t2:55 96 t2:56 97 t2:57 98 t2:58 99 t2:59100 t2:60101 t1:41102 t1:42103 t1:43104 t1:44105 t1:45106 t1:46107 t1:47108 t1:48109 t1:49110 t1:50111 t2:61112 t2:62113 t2:63114 t2:64115 t2:65116 t2:66117 t2:67118 t2:68119 t2:69120 t2:70121 t2:71122 t2:72123 t2:73124 t2:74125 t2:75126 t2:76127 t2:77128 t2:78129 t2:79130 t2:80131 t2:81132 t2:82133 t2:83134 t2:84135 t2:85136 t2:86137 t2:87138 t2:88139 t2:89140 t2:90141 t1:51142 t1:52143 t1:53144 t1:54145 t1:55146 t1:56147 t1:57148 t1:58149 t1:59150 t1:60151 t2:91152 t2:92153 t2:93154 t2:94155 t2:95156 t2:96157 t2:97158 t2:98159 t2:99160 t2:100161 t1:61162 t1:62163 t1:63164 t1:64165 t1:65166 t1:66167 t1:67168 t1:68169 t1:69170 t1:70171 t1:71172 t1:72173 t1:73174 t1:74175 t1:75176 t1:76177 t1:77178 t1:78179 t1:79180 t1:80181 t1:81182 t1:82183 t1:83184 t1:84185 t1:85186 t1:86187 t1:87188 t1:88189 t1:89190 t1:90191 t1:91192 t1:92193 t1:93194 t1:94195 t1:95196 t1:96197 t1:97198 t1:98199 t1:99200 t1:100
View Code
java線程sleep(),join()和yield方法