ABC三个线程分别打印ABC,现实现能够循环N次打印ABC

文章目录

  • V2.0 使用wait()和notifyAll()
    • 2.1 优缺点
    • 2.2 代码
    • Reference
  • V1.0 面试挂的比较low的版本,只使用synchronized
    • 1.1 缺点
    • 1.2 代码

V2.0 使用wait()和notifyAll()

2.1 优缺点

进程可以终止。
便于理解和实现。

2.2 代码

package org.example;

public class SkMain {
    public static void main(String[] args) {
        ABCPrinter printer = new ABCPrinter();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    printer.printA();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    printer.printB();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    printer.printC();
                }
            }
        }).start();
    }
}

class ABCPrinter {
    private volatile int value = 1;

    public void printA() {
        synchronized (this) {
            while(value != 1) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("A");
            value++;
            notifyAll();
        }
    }

    public void printB() {
        synchronized (this) {
            while(value != 2) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("B");
            value++;
            notifyAll();
        }
    }

    public void printC() {
        synchronized (this) {
            while(value != 3) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("C");
            value = 1;
            notifyAll();
        }
    }
}

Reference

https://blog.csdn.net/realYuzhou/article/details/108367264

V1.0 面试挂的比较low的版本,只使用synchronized

1.1 缺点

程序不会终止

1.2 代码

A.java

package org.example;

public class A implements Runnable {
    private B b;

    private C c;

    private int N = 5;

    private boolean isFinish = false;

    @Override
    public void run() {
        while (N > 0) {
            synchronized (c) {
                if (N ==5 && !isFinish) {
                    core();
                } else if (c.isFinish() && !isFinish) { // wait c finish.
                    core();
                }
            }
        }
    }

    private void core() {
        isFinish = false;
        System.out.println("A");
        N--;
        isFinish = true;
        b.setFinish(false);
    }

    public void setFinish(boolean finish) {
        isFinish = finish;
    }

    public boolean isFinish() {
        return isFinish;
    }

    public void setC(C c) {
        this.c = c;
    }

    public void setB(B b) {
        this.b = b;
    }
}

B.java

package org.example;

public class B implements Runnable {
    A a;

    C c;

    private boolean isFinish = false;

    @Override
    public void run() {
        while(true) {
            synchronized (a) {
                if (a.isFinish() && !isFinish) {
                    isFinish = false;
                    System.out.println("B");
                    isFinish = true;
                    c.setFinish(false);
                }
            }
        }
    }

    public boolean isFinish() {
        return isFinish;
    }

    public void setFinish(boolean finish) {
        isFinish = finish;
    }

    public void setA(A a) {
        this.a = a;
    }

    public void setC(C c) {
        this.c = c;
    }
}

C.java

package org.example;

public class C implements Runnable {
    B b;

    A a;

    private boolean isFinish = false;

    @Override
    public void run() {
        while (true) {
            synchronized (b) {
                if (b.isFinish() && !isFinish) {
                    isFinish = false;
                    System.out.println("C");
                    isFinish = true;
                    a.setFinish(false);
                }
            }
        }
    }

    public boolean isFinish() {
        return isFinish;
    }

    public void setFinish(boolean finish) {
        isFinish = finish;
    }

    public void setB(B b) {
        this.b = b;
    }

    public void setA(A a) {
        this.a = a;
    }
}

Main.java

package org.example;

public class Main {
    public static void main(String[] args) {
        A a = new A();
        B b = new B();
        C c = new C();

        b.setA(a);
        a.setB(b);
        c.setB(b);
        c.setA(a);
        b.setC(c);
        a.setC(c);

        Thread threadA = new Thread(a);
        Thread threadB = new Thread(b);
        Thread threadC = new Thread(c);

        threadA.start();
        threadB.start();
        threadC.start();
    }
}

你可能感兴趣的:(Java并发编程,java,开发语言)