多线程拾遗-例题

原题:
利用多线程循环打印A、B、C

思路一:
printA() printB() printC() 三个方法,对this加锁,根据一个flag来控制打印对象。

    public class ABCRE{
    private volatile String flag = "A";//记录当前应该打印的值

    public synchronized void printA() throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            while(!flag.equals("A")){
                wait();
            }
            System.out.print("A");
            flag="B";
            notifyAll();
        }
    }

    public synchronized void printB() throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            while(!flag.equals("B")){
                wait();
            }
            System.out.print("B");
            flag="C";
            notifyAll();
        }
    }

    public synchronized void printC() throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            while(!flag.equals("C")){
                wait();
            }
            System.out.println("C");
            flag="A";
            notifyAll();

        }
    }

    public static void main(String[] args) {
        ABCRE a = new ABCRE();
        new TestThreadA(a).start();
        new TestThreadB(a).start();
        new TestThreadC(a).start();
    }
}

class TestThreadA extends  Thread{
    private ABCRE abcre;
    public TestThreadA(ABCRE a){
        abcre = a;
    }
    @Override
    public void run(){
        try {
            abcre.printA();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class TestThreadB extends  Thread{
    private ABCRE abcre;
    public TestThreadB(ABCRE a){
        abcre = a;
    }
    @Override
    public void run(){
        try {
            abcre.printB();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
class TestThreadC extends  Thread{
    private ABCRE abcre;
    public TestThreadC(ABCRE a){
        abcre = a;
    }
    @Override
    public void run(){
        try {
            abcre.printC();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

思路二
三个现成,各自有各自的flag和print

public class PrintABC{
    public static volatile String flag = "A";

    public static void main(String[] args) {
        Object lock = new Object();
        new ThreadT(lock,"B","A").start();
        new ThreadT(lock,"C","B").start();
        new ThreadT(lock,"A","C").start();
    }
}

class ThreadT extends  Thread{
    private Object lock;
    private String nextFlag;
    private String print;
    public ThreadT(Object lock,String nextFlag,String print){
        this.lock = lock;
        this.print = print;
        this.nextFlag = nextFlag;
    }

    @Override
    public void run(){
        synchronized (lock){
            for (int i = 0; i < 10; i++) {
                while(!print.equals(PrintABC.flag)){
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.print(PrintABC.flag);
                PrintABC.flag = nextFlag;
                lock.notifyAll();
            }
        }
    }
}

原题:
实现生产者消费者模型


猜你喜欢

转载自blog.csdn.net/u012795231/article/details/80706456