Ajin的博客

越努力,越幸运!

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

使用join方法,让三个线程之间存在等待关系

代码如下:

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");
		}
}
运行结果:

A
B
C



阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/whu_zcj/article/details/51546226
个人分类: Java
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭