【多线程基础】- 多个线程顺序打印ABC
生活随笔
收集整理的這篇文章主要介紹了
【多线程基础】- 多个线程顺序打印ABC
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目:3個線程名字分別是A,B,C 現在在console上連續打印10次 ABC 。
public class Test {public static void main(String[] args) {Thread a = new Thread(new Task("A",0));Thread b = new Thread(new Task("B",1));Thread c = new Thread(new Task("C",2));a.start();b.start();c.start();}static class Task implements Runnable{private String one;private int count;private static int num = 0;public Task(String one,int count){this.one = one;this.count = count;}@Overridepublic void run() {int i = 0;while(i < 10){synchronized(Task.class){if(num % 3 == count){num++;System.out.println(one);}else{continue;}}i++;}}} }?
轉載于:https://www.cnblogs.com/lixusign/p/3352480.html
總結
以上是生活随笔為你收集整理的【多线程基础】- 多个线程顺序打印ABC的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 师者所以传道授业解惑也出自哪里
- 下一篇: 攻城掠地攻略:战车数据大全