2020-10-27 多线程学习4

39 阅读1分钟

三个线程分别打印A B C ,循环打印五遍,结果为 ABCABCABCABC;
分析:每个线程固定做打印的事情,并且有一定顺序,那么需要一个变量通知,即 A结束通知B (A>B),B>C,C>A.....

下面给出两种解法(其实是一种):
基于sync

// 基于sync
public class wubian {

    private int flag = 1;

    public static void main(String[] args) {
        wubian wubian = new wubian();

        Thread a = new Thread(() -> {
            wubian.aprint();
        });
        Thread b = new Thread(() -> {
            wubian.bprint();
        });
        Thread c = new Thread(() -> {
            wubian.cprint();
        });

        a.setName("A");
        b.setName("B");
        c.setName("C");

        a.start();
        b.start();
        c.start();

        /**只打印一边的话,可以用join**/
    }

    public synchronized void aprint() {
        for (int i = 0; i < 5; i++) {
            while (flag != 1) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName());
            flag = 2;
            this.notifyAll();
        }
    }

    public synchronized void bprint() {
        for (int i = 0; i < 5; i++) {
            while (flag != 2) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName());
            flag = 3;
            this.notifyAll();
        }
    }

    public synchronized void cprint() {
        for (int i = 0; i < 5; i++) {
            while (flag != 3) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName());
            flag = 1;
            this.notifyAll();
        }
    }


}

第二种 基于lock

// 基于lock
public class wubian1 {
    private int flag = 1;
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();
    public static void main(String[] args) {
        wubian1 wubian = new wubian1();
        Thread a = new Thread(() -> {
            wubian.aprint();
        });
        Thread b = new Thread(() -> {
            wubian.bprint();
        });
        Thread c = new Thread(() -> {
            wubian.cprint();
        });
        a.setName("A");b.setName("B");c.setName("C");
        a.start();b.start();c.start();

        /**只打印一边的话,可以用join**/
    }

    public void aprint() {
        for (int i = 0; i < 5; i++) {
            try {
                lock.lock();
                while (flag != 1) {
                    try {
                        condition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName());
                flag = 2;
                condition.signal();
            } finally {
                lock.unlock();
            }
        }

    }

    public void bprint() {
        for (int i = 0; i < 5; i++) {
            try {
                lock.lock();
                while (flag != 2) {
                    try {
                        condition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName());
                flag = 3;
                condition.signal();
            } finally {
                lock.unlock();
            }
        }
    }

    public void cprint() {

        for (int i = 0; i < 5; i++) {
            try {
                lock.lock();
                while (flag != 3) {
                    try {
                        condition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName());
                flag = 1;
                condition.signalAll();
            } finally {
                lock.unlock();
            }
        }
    }


}

上面两种几乎没有区别,仅仅是lock与sync 的区别

本文转自 jimolvxing.blog.csdn.net/article/det…,如有侵权,请联系删除。