您的位置:首页 > 其它

多线程顺序打印ABC的三种实现---join方法

2016-08-05 10:03 696 查看
使用join方法,让三个线程之间存在等待关系

代码如下:

[java] view
plain copy

 





package com.zcj.join;  

  

public class JoinTest {  

    public static void main(String[] args) {  

        ThreadA threadA = new ThreadA();  

        ThreadB threadB = new ThreadB(threadA);  

        ThreadC threadC = new ThreadC(threadB);  

        threadA.start();  

        threadB.start();  

        threadC.start();  

    }  

}  

  

class ThreadA extends Thread{  

    @Override  

    public void run() {  

        // TODO Auto-generated method stub  

        System.out.println("A");  

    }  

      

}  

class ThreadB extends Thread{  

     private ThreadA threadA;  

       public ThreadB(ThreadA threadA){  

           this.threadA =threadA;  

       }  

        @Override  

        public void run() {  

            // TODO Auto-generated method stub  

            try {  

                threadA.join();  

            } catch (InterruptedException e) {  

                // TODO Auto-generated catch block  

                e.printStackTrace();  

            }  

            System.out.println("B");  

        }  

          

    }  

class ThreadC extends Thread{  

       private ThreadB threadB;  

       public ThreadC(ThreadB threadB){  

           this.threadB =threadB;  

       }  

        @Override  

        public void run() {  

            // TODO Auto-generated method stub  

            try {  

                threadB.join();  

            } catch (InterruptedException e) {  

                // TODO Auto-generated catch block  

                e.printStackTrace();  

            }  

            System.out.println("C");  

        }  

}  

运行结果:

[plain] view
plain copy

 





A  

B  

C  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: