Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4293bc1

Browse files
committed
join方法
1 parent 27d0fbd commit 4293bc1

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.zhxh.codeproj.multithread.join;
2+
3+
4+
class JoinDemo implements Runnable {
5+
6+
@Override
7+
public void run() {
8+
for (int i = 0; i < 10; i++) {
9+
System.out.println("线程1第" + i + "次执行!");
10+
}
11+
}
12+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.zhxh.codeproj.multithread.join;
2+
3+
4+
/*
5+
join()   join()方法会使当前线程等待调用join()方法的线程结束后才能继续执行,例如:
6+
*/
7+
public class JoinTest {
8+
public static void main(String[] args) {
9+
Thread thread = new Thread(new JoinDemo());
10+
thread.start();
11+
12+
for (int i = 0; i < 20; i++) {
13+
System.out.println("主线程第" + i + "次执行!");
14+
if (i >= 2)
15+
try {
16+
// t1线程合并到主线程中,主线程停止执行过程,转而执行t1线程,直到t1执行完毕后继续。
17+
thread.join();
18+
} catch (InterruptedException e) {
19+
e.printStackTrace();
20+
}
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)