关闭

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

823人阅读 评论(0) 收藏 举报
分类:

使用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



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:60775次
    • 积分:1805
    • 等级:
    • 排名:千里之外
    • 原创:123篇
    • 转载:14篇
    • 译文:0篇
    • 评论:6条
    最新评论