Java_如何等待子线程执行结束
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
42
43
44
45
46
47
48
49
50
|
</div> public class Threads { public static void main(String[] args) { SubThread thread = new SubThread(); thread.start(); //主线程处理其他工作,让子线程异步去执行. mainThreadOtherWork(); System.out.println( "now waiting sub thread done." ); //主线程其他工作完毕,等待子线程的结束, 调用join系列的方法即可(可以设置超时时间) try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "now all done." ); } private static void mainThreadOtherWork() { System.out.println( "main thread work start" ); try { Thread.sleep(3000L); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "main thread work done." ); } public static class SubThread extends Thread{ @Override public void run() { working(); } private void working() { System.out.println( "sub thread start working." ); busy(); System.out.println( "sub thread stop working." ); } private void busy() { try { sleep(5000L); } catch (InterruptedException e) { e.printStackTrace(); } } } } |
- main thread work start
- sub thread start working.
- main thread work done.
- now waiting sub thread done.
- sub thread stop working.
- now all done.
1
2
3
|
public final void join() throws InterruptedException { join( 0 ); } |
public final synchronized void join(long millis)
1
2
3
|
while (isAlive()) { wait( 0 ); } |
Tests if this thread is alive. A thread is alive if it has been started and has not yet died.
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
42
43
44
45
46
47
48
49
50
51
52
53
54
|
public class Threads { static ExecutorService executorService = Executors.newFixedThreadPool( 1 ); @SuppressWarnings ( "rawtypes" ) public static void main(String[] args) throws InterruptedException, ExecutionException { SubThread thread = new SubThread(); // thread.start(); Future future = executorService.submit(thread); mainThreadOtherWork(); System.out.println( "now waiting sub thread done." ); future.get(); // try { // thread.join(); // } catch (InterruptedException e) { // e.printStackTrace(); // } System.out.println( "now all done." ); executorService.shutdown(); } private static void mainThreadOtherWork() { System.out.println( "main thread work start" ); try { Thread.sleep(3000L); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "main thread work done." ); } public static class SubThread extends Thread{ @Override public void run() { working(); } private void working() { System.out.println( "sub thread start working." ); busy(); System.out.println( "sub thread stop working." ); } private void busy() { try { sleep(5000L); } catch (InterruptedException e) { e.printStackTrace(); } } } } |
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
public class Threads { // static ExecutorService executorService = Executors.newFixedThreadPool(1); static final BlockingQueue queue = new ArrayBlockingQueue( 1 ); public static void main(String[] args) throws InterruptedException, ExecutionException { SubThread thread = new SubThread(queue); thread.start(); // Future future = executorService.submit(thread); mainThreadOtherWork(); System.out.println( "now waiting sub thread done." ); // future.get(); queue.take(); // try { // thread.join(); // } catch (InterruptedException e) { // e.printStackTrace(); // } System.out.println( "now all done." ); // executorService.shutdown(); } private static void mainThreadOtherWork() { System.out.println( "main thread work start" ); try { Thread.sleep(3000L); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "main thread work done." ); } public static class SubThread extends Thread{ private BlockingQueue queue; /** * @param queue */ public SubThread(BlockingQueue queue) { this .queue = queue; } @Override public void run() { try { working(); } finally { try { queue.put( 1 ); } catch (InterruptedException e) { e.printStackTrace(); } } } private void working() { System.out.println( "sub thread start working." ); busy(); System.out.println( "sub thread stop working." ); } private void busy() { try { sleep(5000L); } catch (InterruptedException e) { e.printStackTrace(); } } } } |
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
public class Threads { // static ExecutorService executorService = Executors.newFixedThreadPool(1); static final BlockingQueue queue = new ArrayBlockingQueue( 1 ); public static void main(String[] args) throws InterruptedException, ExecutionException { int threads = 5 ; CountDownLatch countDownLatch = new CountDownLatch(threads); for ( int i= 0 ;i<threads;i++){ SubThread thread = new SubThread( 2000 *(i+ 1 ), countDownLatch); thread.start(); } // Future future = executorService.submit(thread); mainThreadOtherWork(); System.out.println( "now waiting sub thread done." ); // future.get(); // queue.take(); countDownLatch.await(); // try { // thread.join(); // } catch (InterruptedException e) { // e.printStackTrace(); // } System.out.println( "now all done." ); // executorService.shutdown(); } private static void mainThreadOtherWork() { System.out.println( "main thread work start" ); try { Thread.sleep(3000L); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "main thread work done." ); } public static class SubThread extends Thread{ // private BlockingQueue queue; private CountDownLatch countDownLatch; private long work; /** * @param queue */ // public SubThread(BlockingQueue queue) { // this.queue = queue; // this.work = 5000L; // } public SubThread( long work, CountDownLatch countDownLatch) { // this.queue = queue; this .countDownLatch = countDownLatch; this .work = work; } @Override public void run() { try { working(); } finally { // try { // queue.put(1); // } catch (InterruptedException e) { // e.printStackTrace(); // } countDownLatch.countDown(); } } private void working() { System.out.println(getName()+ " sub thread start working." ); busy(); System.out.println(getName()+ " sub thread stop working." ); } private void busy() { try { sleep(work); } catch (InterruptedException e) { e.printStackTrace(); } } } } |
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。