blob: 60791e4450198ad602008071d421c2f3ed37d114 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
jeffhao5d1ac922011-09-29 17:41:15 -070016
Andreas Gampe5ae7e6b2016-11-10 13:40:32 -080017import java.util.concurrent.CountDownLatch;
18
jeffhao5d1ac922011-09-29 17:41:15 -070019/**
20 * Make sure that a sub-thread can join the main thread.
21 */
22public class Main {
Andreas Gampe5ae7e6b2016-11-10 13:40:32 -080023 public static void main(String[] args) throws Exception {
jeffhao5d1ac922011-09-29 17:41:15 -070024 Thread t;
Andreas Gampe5ae7e6b2016-11-10 13:40:32 -080025 CountDownLatch waitLatch = new CountDownLatch(1);
26 CountDownLatch progressLatch = new CountDownLatch(1);
jeffhao5d1ac922011-09-29 17:41:15 -070027
Andreas Gampe5ae7e6b2016-11-10 13:40:32 -080028 t = new Thread(new JoinMainSub(Thread.currentThread(), waitLatch, progressLatch), "Joiner");
jeffhao5d1ac922011-09-29 17:41:15 -070029 System.out.print("Starting thread '" + t.getName() + "'\n");
30 t.start();
31
Andreas Gampe5ae7e6b2016-11-10 13:40:32 -080032 waitLatch.await();
jeffhao5d1ac922011-09-29 17:41:15 -070033 System.out.print("JoinMain starter returning\n");
Andreas Gampe5ae7e6b2016-11-10 13:40:32 -080034 progressLatch.countDown();
35
36 // Keep the thread alive a little longer, giving the other thread a chance to join on a
37 // live thread (though that isn't critically important for the test).
38 Thread.currentThread().sleep(500);
jeffhao5d1ac922011-09-29 17:41:15 -070039 }
40}
41
42class JoinMainSub implements Runnable {
43 private Thread mJoinMe;
Andreas Gampe5ae7e6b2016-11-10 13:40:32 -080044 private CountDownLatch waitLatch;
45 private CountDownLatch progressLatch;
jeffhao5d1ac922011-09-29 17:41:15 -070046
Andreas Gampe5ae7e6b2016-11-10 13:40:32 -080047 public JoinMainSub(Thread joinMe, CountDownLatch waitLatch, CountDownLatch progressLatch) {
jeffhao5d1ac922011-09-29 17:41:15 -070048 mJoinMe = joinMe;
Andreas Gampe5ae7e6b2016-11-10 13:40:32 -080049 this.waitLatch = waitLatch;
50 this.progressLatch = progressLatch;
jeffhao5d1ac922011-09-29 17:41:15 -070051 }
52
53 public void run() {
54 System.out.print("@ JoinMainSub running\n");
55
56 try {
Andreas Gampe5ae7e6b2016-11-10 13:40:32 -080057 waitLatch.countDown();
58 progressLatch.await();
jeffhao5d1ac922011-09-29 17:41:15 -070059 mJoinMe.join();
60 System.out.print("@ JoinMainSub successfully joined main\n");
61 } catch (InterruptedException ie) {
62 System.out.print("@ JoinMainSub interrupted!\n");
63 }
64 finally {
65 System.out.print("@ JoinMainSub bailing\n");
66 }
67 }
68}