一個比較簡單的例子
對公共資源加鎖,以阻塞其它線程。
用一個全局變量(3個線程都可訪問的公共變量)控制狀態
/** * 有三個線程名字分別是A、B、C,每個線程只能打印自己的名字,在屏幕上順序打印 ABC,打印10次 * * @author chenhening * @date 2017年3月16日 */ public class MyABC extends Thread { private static Object o = new Object();// 對象鎖,必須是static private static int count = 0;// 控制輸出哪個字母 private char ID;// 字母 private int id;// 字母對應的數字 private int num = 0;// 打印次數 public MyABC(int id, char ID) { this.id = id; this.ID = ID; } @Override public void run() { synchronized (o) { while (num < 10) { if (count % 3 == id) { System.out.print(ID); ++count; ++num; // 喚醒所有等待線程 o.notifyAll(); } else { try { // 如果不滿足條件,則阻塞等待 o.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } public static void main(String[] args) { (new MyABC(0, 'A')).start(); (new MyABC(1, 'B')).start(); (new MyABC(2, 'C')).start(); } }
第二種方法:
/** * 有三個線程名字分別是A、B、C,每個線程只能打印自己的名字,在屏幕上順序打印 ABC,打印10次 * * @author chenhening * @date 2017年3月16日 */ public class SwitchStatus extends Thread { private static String currentThread = "A";// 初始為A private static Object lock = new Object();// 作為鎖 private String name = ""; private int count = 10;// 打印次數 public SwitchStatus(String name) { this.name = name; } @Override public void run() { while (count > 0) { synchronized (lock) { if (currentThread.equals(this.name)) { System.out.print(name); count--; if (currentThread.equals("A")) { currentThread = "B"; } else if (currentThread.equals("B")) { currentThread = "C"; } else if (currentThread.equals("C")) { currentThread = "A"; System.out.println(); } } } } } public static void main(String[] args) { (new SwitchStatus("A")).start(); (new SwitchStatus("B")).start(); (new SwitchStatus("C")).start(); } }
第三種:
/** * 有三個線程名字分別是A、B、C,每個線程只能打印自己的名字,在屏幕上順序打印 ABC,打印10次 * * @author chenhening * @date 2017年3月16日 */ public class SleepExample extends Thread { private static int currentCount = 0; public SleepExample(String name) { // 設置線程的名稱 this.setName(name); } @Override public void run() { while (currentCount < 30) {// 10遍*3個字母 switch (currentCount % 3) {// 除以3求余數 case 0: if ("A".equals(getName())) { System.out.print(getName()); currentCount++; } break; case 1: if ("B".equals(getName())) { System.out.print(getName()); currentCount++; } break; case 2: if ("C".equals(getName())) { System.out.print(getName()); currentCount++; } break; } } } public static void main(String[] args) { // 線程啟動順序無關 new SleepExample("A").start(); new SleepExample("B").start(); new SleepExample("C").start(); } }