线程t1|线程t1,t2,t3 分别输出A,B,C 要求循环输出ABC 10次

public class DemoTest{ private static String[] flag = {"A"}; static class AThread extends Thread{ @Override public void run(){ int i=0; while (i<10){ synchronized (flag){ if(flag[0].equals("A")){ System.out.print("A"); flag[0]="B"; i++; } flag.notify(); try { flag.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } }static class BThread extends Thread{ @Override public void run(){ int i=0; while (i<10){ synchronized (flag){ if(flag[0].equals("B")){ System.out.print("B"); flag[0]="C"; i++; } flag.notify(); try { flag.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } }static class CThread extends Thread{ @Override public void run(){ int i=0; while (i<10){ synchronized (flag){ if(flag[0].equals("C")){ System.out.println("C"); flag[0]="A"; i++; } flag.notify(); try { flag.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } }public static void main(String[] args) { AThread t1 = new AThread(); BThread t2 = new BThread(); CThread t3 = new CThread(); t1.start(); t2.start(); t3.start(); } }

    推荐阅读