-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadOrderRun1.java
More file actions
41 lines (38 loc) · 1.14 KB
/
Copy pathThreadOrderRun1.java
File metadata and controls
41 lines (38 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package thread;
import org.junit.Test;
/**
* @Author: wei1
* @Date: Create in 2018/10/11 20:47
* @Description: 使得多线程按顺序执行
* 思路:使用join
*/
public class ThreadOrderRun1 {
private void order(int n) throws InterruptedException {
Thread tmp;
for (int i = 0; i < n; i++) {
tmp = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName());
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "thread--" + i);
tmp.start();
//当我们调用某个线程的这个方法时,这个方法会挂起调用线程,
//直到被调用线程结束执行,调用线程才会继续执行。
tmp.join();
}
}
@Test
public void testOrder() {
try {
order(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}