多线程顺序打印ABC的三种实现---join方法
【多线程顺序打印ABC的三种实现---join方法】 使用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
推荐阅读
- 放屁有这三个特征的,请注意啦!这说明你的身体毒素太多
- 爱就是希望你好好活着
- 昨夜小楼听风
- 知识
- 死结。
- 我从来不做坏事
- 烦恼和幸福
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- Linux下面如何查看tomcat已经使用多少线程
- 说得清,说不清