两种方案:
        1>利用Thread.join()方法,使C进程等待AB进程完成后执行
        2>利用CountdownLatch定义一个计数器,在AB进程里用CountdownLatch. countDown()方法使计数器减少,在等待进程C中使用CountDownLatch.await()方法等待,直到计数器变为0,才开始执行
1.    思路:
a)    建立A B C三个线程,空跑模拟线程运行。
b)    在父进程中通过start()启动各子线程。
c)    利用上述两种方案完成任务。
2.    代码:
TestThread.java

  1. public class TestThread {
  2.     public static void main(String[] args){
  3.         Thread A=new Thread(new Runnable() {
  4.             @Override
  5.             public void run() {
  6.                 int i=0;
  7.                 while(i<18){
  8.                     try {
  9.                         Thread.sleep(1000);
  10.                     }catch (Exception e){
  11.                         e.printStackTrace();
  12.                     }
  13.                     i++;
  14.                 }
  15.                 System.out.println("A OK");
  16.             }
  17.         });
  18.         Thread B=new Thread(new Runnable() {
  19.             @Override
  20.             public void run() {
  21.                 int i=0;
  22.                 while(i<10){
  23.                     try {
  24.                         Thread.sleep(1000);
  25.                     }catch (Exception e){
  26.                         e.printStackTrace();
  27.                     }
  28.                     i++;
  29.                 }
  30.                 System.out.println("B OK");
  31.             }
  32.         });
  33.         Thread C=new Thread(new Runnable() {
  34.             @Override
  35.             public void run() {
  36.                 try {
  37.                     A.join();
  38.                     B.join();
  39.                 }
  40.                 catch (Exception e) {
  41.                     e.printStackTrace();
  42.                 }
  43.                 int i=0;
  44.                 while(i<6){
  45.                     try {
  46.                         Thread.sleep(1000);
  47.                     }catch (Exception e){
  48.                         e.printStackTrace();
  49.                     }
  50.                     i++;
  51.                 }
  52.                 System.out.println("C OK");
  53.             }
  54.         });
  55.         A.start();
  56.         B.start();
  57.         C.start();
  58.     }
  59. }


TestThread2.java

  1. import java.util.concurrent.CountDownLatch;
  2. public class TestThread2 {
  3.     public static void main(String[] args){
  4.         CountDownLatch count=new CountDownLatch(2);
  5.         new Thread(new Runnable() {
  6.             @Override
  7.             public void run() {
  8.                 int i=0;
  9.                 while(i<18){
  10.                     try {
  11.                         Thread.sleep(1000);
  12.                     }catch (Exception e){
  13.                         e.printStackTrace();
  14.                     }
  15.                     i++;
  16.                 }
  17.                 System.out.println("A OK");
  18.                 count.countDown();
  19.             }
  20.         }).start();
  21.         new Thread(new Runnable() {
  22.             @Override
  23.             public void run() {
  24.                 int i=0;
  25.                 while(i<10){
  26.                     try {
  27.                         Thread.sleep(1000);
  28.                     }catch (Exception e){
  29.                         e.printStackTrace();
  30.                     }
  31.                     i++;
  32.                 }
  33.                 System.out.println("B OK");
  34.                 count.countDown();
  35.             }
  36.         }).start();
  37.         new Thread(new Runnable() {
  38.             @Override
  39.             public void run() {
  40.                 try {
  41.                     count.await();
  42.                 }
  43.                 catch (Exception e) {
  44.                     e.printStackTrace();
  45.                 }
  46.                 int i=0;
  47.                 while(i<6){
  48.                     try {
  49.                         Thread.sleep(1000);
  50.                     }catch (Exception e){
  51.                         e.printStackTrace();
  52.                     }
  53.                     i++;
  54.                 }
  55.                 System.out.println("C OK");
  56.             }
  57.         }).start();
  58.     }
  59. }