码迷,mamicode.com
首页 > 编程语言 > 详细

java线程sleep(),join()和yield方法

时间:2015-04-06 21:34:59      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

 

 

 

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:59
100 t2:60
101 t1:41
102 t1:42
103 t1:43
104 t1:44
105 t1:45
106 t1:46
107 t1:47
108 t1:48
109 t1:49
110 t1:50
111 t2:61
112 t2:62
113 t2:63
114 t2:64
115 t2:65
116 t2:66
117 t2:67
118 t2:68
119 t2:69
120 t2:70
121 t2:71
122 t2:72
123 t2:73
124 t2:74
125 t2:75
126 t2:76
127 t2:77
128 t2:78
129 t2:79
130 t2:80
131 t2:81
132 t2:82
133 t2:83
134 t2:84
135 t2:85
136 t2:86
137 t2:87
138 t2:88
139 t2:89
140 t2:90
141 t1:51
142 t1:52
143 t1:53
144 t1:54
145 t1:55
146 t1:56
147 t1:57
148 t1:58
149 t1:59
150 t1:60
151 t2:91
152 t2:92
153 t2:93
154 t2:94
155 t2:95
156 t2:96
157 t2:97
158 t2:98
159 t2:99
160 t2:100
161 t1:61
162 t1:62
163 t1:63
164 t1:64
165 t1:65
166 t1:66
167 t1:67
168 t1:68
169 t1:69
170 t1:70
171 t1:71
172 t1:72
173 t1:73
174 t1:74
175 t1:75
176 t1:76
177 t1:77
178 t1:78
179 t1:79
180 t1:80
181 t1:81
182 t1:82
183 t1:83
184 t1:84
185 t1:85
186 t1:86
187 t1:87
188 t1:88
189 t1:89
190 t1:90
191 t1:91
192 t1:92
193 t1:93
194 t1:94
195 t1:95
196 t1:96
197 t1:97
198 t1:98
199 t1:99
200 t1:100
View Code

 

java线程sleep(),join()和yield方法

标签:

原文地址:http://www.cnblogs.com/SHICAI/p/4396579.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!