第一种方法:通过共享对象锁的方式实现
记住调用wait方法时候需要在同步代码块内,否则会报java.lang.IllegalMonitorStateException异常!

package com.test;class Prints {public int num = 0;synchronized public void methodA() throws InterruptedException{while(num!=0){wait();}System.out.println(Thread.currentThread().getName()+":"+num);num = 1;notifyAll();}synchronized public void methodB() throws InterruptedException{while(num!=1){wait();}System.out.println(Thread.currentThread().getName()+":"+num);num = 2;notifyAll();}synchronized public void methodC() throws InterruptedException{while(num!=2){wait();}System.out.println(Thread.currentThread().getName()+":"+num);notifyAll();}
}
class ThreadClassA implements Runnable{private Prints p;public ThreadClassA(Prints p) {this.p = p;}@Overridepublic void run() {try {p.methodA();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
class ThreadClassB implements Runnable{private Prints p;public ThreadClassB(Prints p) {this.p = p;}@Overridepublic void run() {try {p.methodB();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
class ThreadClassC implements Runnable{private Prints p;public ThreadClassC(Prints p) {this.p = p;}@Overridepublic void run() {try {p.methodC();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
public class MainDemo{public static void main(String[] args) throws CloneNotSupportedException {Prints p = new Prints();ThreadClassA a = new ThreadClassA(p);ThreadClassB b = new ThreadClassB(p);ThreadClassC c = new ThreadClassC(p);Thread t1 = new Thread(a);Thread t2 = new Thread(b);Thread t3 = new Thread(c);t1.start();t2.start();t3.start();}
}

第二种方法:通过主线程Join(),join的理解:https://www.cnblogs.com/lcplcpjava/p/6896904.html

package com.test;class Prints {public int num = 0;synchronized public void methodA() throws InterruptedException{while(num!=0){wait();}System.out.println(Thread.currentThread().getName()+":"+num);num = 1;notifyAll();}synchronized public void methodB() throws InterruptedException{while(num!=1){wait();}System.out.println(Thread.currentThread().getName()+":"+num);num = 2;notifyAll();}synchronized public void methodC() throws InterruptedException{while(num!=2){wait();}System.out.println(Thread.currentThread().getName()+":"+num);notifyAll();}
}
class ThreadClassA implements Runnable{private Prints p;public ThreadClassA(Prints p) {this.p = p;}@Overridepublic void run() {
//      synchronized (Thread.currentThread()) {System.out.println("1");
//      }
/*      try {p.methodA();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}*/}}
class ThreadClassB implements Runnable{private Prints p;public ThreadClassB(Prints p) {this.p = p;}@Overridepublic void run() {System.out.println("2");
/*      try {p.methodB();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}*/}}
class ThreadClassC implements Runnable{private Prints p;public ThreadClassC(Prints p) {this.p = p;}@Overridepublic void run() {System.out.println("3");
/*      try {p.methodC();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}*/}}
public class MainDemo{public static void main(String[] args) throws CloneNotSupportedException, InterruptedException {Prints p = new Prints();ThreadClassA a = new ThreadClassA(p);ThreadClassB b = new ThreadClassB(p);ThreadClassC c = new ThreadClassC(p);Thread t1 = new Thread(a);Thread t2 = new Thread(b);Thread t3 = new Thread(c);t1.start();t1.join();t2.start();t2.join();t3.start();t3.join();}
}

第三种:通过线程执行时调用join方法

package com.test;import java.util.Random;class T1 extends Thread {public void run(){Random random = new Random();try {Thread.sleep(random.nextInt(1000));} catch (InterruptedException e) {e.printStackTrace();}System.out.println("in T1");}
}class T2 extends Thread{private Thread thread;public T2(Thread thread) {this.thread = thread;}public void run(){try {thread.join();} catch (InterruptedException e) {e.printStackTrace();}System.out.println("in T2");}
}class T3 extends Thread{private Thread thread;public T3(Thread thread) {this.thread = thread;}public void run(){try {thread.join();} catch (InterruptedException e) {e.printStackTrace();}System.out.println("in T3");}
}
public class MainDemo{public static void main(String[] args) throws CloneNotSupportedException, InterruptedException {T1 t1 = new T1();T2 t2 = new T2(t1);T3 t3 = new T3(t2);t2.start();t1.start();t3.start();}
}

设置三个线程顺序打印数字问题(转载)相关推荐

  1. 常见的面试算法题:创建几个线程按顺序打印数字或者字母(多线程编程)(套用该模板即可)

    常见的面试算法题:创建几个线程按顺序打印数字或者字母(多线程编程)(套用该模板即可) 比较典型的题目,如下有: 题目一: 1.启动3个线程打印递增的数字, 线程1先打印1,2,3,4,5, 然后是线程 ...

  2. java多线程交替打印_使用Java实现三个线程交替打印0-74

    使用Java实现三个线程交替打印0-74 题目分析 三个线程交替打印,即3个线程是按顺序执行的.一个线程执行完之后,唤醒下一个线程,然后阻塞,等待被该线程的上一个线程唤醒.执行的顺序是一个环装的队列 ...

  3. java 线程交替输出,[java]java经典问题之线程交替打印数字

    问题 给出两个线程,要求两个线程交替打印从1到100,例如:A线程打印1,B线程打印2,A线程打印3...依次类推,直到打印到100 思路 这里主要是考察对java中wait/notifyAll机制的 ...

  4. 三个线程交替打印ABC(Condition实现精确通知)

    三个线程交替打印ABC: package pc;import java.util.concurrent.locks.Condition; import java.util.concurrent.loc ...

  5. C和指针之函数之归以字符形式按顺序打印数字的每位数字(递归和非递归)

    1.问题 以字符形式按顺序打印数字的每位数字(递归和非递) 2.代码实现 #include <stdio.h> #include <math.h> //以字符形式按顺序打印数字 ...

  6. java三个线程 顺序执行_如何确保三个线程顺序执行

    三个线程t1.t2.t3.确保三个线程,t1执行完后t2执行,t2执行完后t3执行. 一.使用join thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程. ...

  7. 两种方式实现线程通信:三个线程交替打印AABBCC

    多线程之间是抢占资源的,使用线程通信可以达到线程按序执行的目的 线程共享资源类, 首先创建一个资源类, 包含三个打印的方法以及首次打印的字符串 多个线程访问,方法加synchronized同步锁 cl ...

  8. java 多线程输出_[Java多线程]ABC三个线程顺序输出的问题

    大概的问题是这样的: 有A,B,C三个线程, A线程输出A, B线程输出B, C线程输出C 要求, 同时启动三个线程, 按顺序输出ABC, 循环10次 这是一个多线程协同的问题, 本身多线程是没有执行 ...

  9. 三个不同线程顺序打印ABC十种写法,看到就是赚到!

    夫陶公清风千古,余又何人,敢称庶几 个人博客地址:http://www.breez.work

最新文章

  1. 【DIY】填坑,热水器自动定时烧水断电方案,预期目标及功能
  2. c语言编译成功,[C/CPP系列知识] 那些程序C语言可以编译通过但C++无法编译成功 Write a C program that won’t compile in C++...
  3. 值得收藏的时间复杂度速查表:数据结构操作、排序算法、图操作、堆操作
  4. Everything is Serverless,从开源框架对比说起
  5. 上采样和下采样_OpenCV学习笔记(一)之图像金字塔-上采样与降采样与DOG
  6. React Native 交互管理器InteractionManager
  7. linux环境下启动git,linux系统安装git及git常用命令
  8. windows10下搭建spark平台
  9. C# WinForm制作登录界面
  10. Tomat报错 The APR based Apache Tomcat Native library which allows optimal performance in production
  11. 人类创造的工具是不是人工智能?
  12. html图片标签 imag
  13. 2177 找到和为给定整数的三个连续整数(思维题)
  14. RuntimeError: grad can be implicitly created only for scalar outputs
  15. 实验二 同步时序方式设计_秒表
  16. 碳纳米角、纳米囊泡等量子点复合纳米材料的定制合成方法
  17. Jmeter模拟上传图片
  18. 绝对布局(AbsoluteLayout)的简单使用
  19. The temporary upload location [C:\Users\test\AppData\Local\Temp\tomcat.8083403186712289847.8080\报错
  20. 云计算中心如何存储数据

热门文章

  1. linux脚本实现红绿灯,javascript 如何实现红绿灯效果呢?
  2. 软件工程 - 团队重组
  3. 现代软件工程系列 学生和老师都不容易
  4. html留言板 php,linux下使用Apache+php实现留言板功能的网站
  5. Linux的实际操作:用户管理(查ls -ahl,chown改文件所属者,chgrp改文件所属组,usermod改用户所属组)
  6. python代码自动生成器下载_Python代码生成器
  7. chrome控制台如何把vw显示成px_你可能不知道的chrome调试技巧
  8. 怎么创建数据表的实体类和业务类_微服务项目第13天:商品分类业务的实现
  9. python画三维立体图难吗_万万没想到,Python竟能绘制出如此酷炫的三维图
  10. linux某个线程信号唤醒,linux多线程编程--信号量和条件变量 唤醒丢失事件