File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed
app/src/main/java/com/zhxh/codeproj/multithread/join Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments