Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 16 | |
Andreas Gampe | 5ae7e6b | 2016-11-10 13:40:32 -0800 | [diff] [blame] | 17 | import java.util.concurrent.CountDownLatch; |
| 18 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 19 | /** |
| 20 | * Make sure that a sub-thread can join the main thread. |
| 21 | */ |
| 22 | public class Main { |
Andreas Gampe | 5ae7e6b | 2016-11-10 13:40:32 -0800 | [diff] [blame] | 23 | public static void main(String[] args) throws Exception { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 24 | Thread t; |
Andreas Gampe | 5ae7e6b | 2016-11-10 13:40:32 -0800 | [diff] [blame] | 25 | CountDownLatch waitLatch = new CountDownLatch(1); |
| 26 | CountDownLatch progressLatch = new CountDownLatch(1); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 27 | |
Andreas Gampe | 5ae7e6b | 2016-11-10 13:40:32 -0800 | [diff] [blame] | 28 | t = new Thread(new JoinMainSub(Thread.currentThread(), waitLatch, progressLatch), "Joiner"); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 29 | System.out.print("Starting thread '" + t.getName() + "'\n"); |
| 30 | t.start(); |
| 31 | |
Andreas Gampe | 5ae7e6b | 2016-11-10 13:40:32 -0800 | [diff] [blame] | 32 | waitLatch.await(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 33 | System.out.print("JoinMain starter returning\n"); |
Andreas Gampe | 5ae7e6b | 2016-11-10 13:40:32 -0800 | [diff] [blame] | 34 | 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); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
| 42 | class JoinMainSub implements Runnable { |
| 43 | private Thread mJoinMe; |
Andreas Gampe | 5ae7e6b | 2016-11-10 13:40:32 -0800 | [diff] [blame] | 44 | private CountDownLatch waitLatch; |
| 45 | private CountDownLatch progressLatch; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 46 | |
Andreas Gampe | 5ae7e6b | 2016-11-10 13:40:32 -0800 | [diff] [blame] | 47 | public JoinMainSub(Thread joinMe, CountDownLatch waitLatch, CountDownLatch progressLatch) { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 48 | mJoinMe = joinMe; |
Andreas Gampe | 5ae7e6b | 2016-11-10 13:40:32 -0800 | [diff] [blame] | 49 | this.waitLatch = waitLatch; |
| 50 | this.progressLatch = progressLatch; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | public void run() { |
| 54 | System.out.print("@ JoinMainSub running\n"); |
| 55 | |
| 56 | try { |
Andreas Gampe | 5ae7e6b | 2016-11-10 13:40:32 -0800 | [diff] [blame] | 57 | waitLatch.countDown(); |
| 58 | progressLatch.await(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 59 | 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 | } |