JUC学习 共享模型 04

同步模式之顺序控制

        固定运行顺序

        

public static final Object lock = new Object();
    public static void main(String[] args) {
        Thread thread1 = new Thread(()->{
            while (true){
                synchronized (lock){
                    try {
                        Thread.sleep(1000);
                        log.debug("1");
                        lock.notify();
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }finally {

                    }
                }
            }
        });
        Thread thread2 = new Thread(()->{
            while (true){
                synchronized (lock){
                    try {
                        Thread.sleep(1000);
                        log.debug("2");
                        lock.notify();
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }finally {

                    }
                }
            }
        });
        thread1.start();
        thread2.start();
    }

交替输出

Lock公平锁

public static final ReentrantLock lock = new ReentrantLock(true);
    public static void main(String[] args) {
        Thread thread1 = new Thread(()->{
            for (int i = 0; i < 5; i++) {
                try {
                    lock.lock();
                    System.out.print("a");
                }finally {
                    lock.unlock();
                }
            }

        });
        Thread thread2 = new Thread(()->{
            for (int i = 0; i < 5; i++) {
                try {
                    lock.lock();
                    System.out.print("b");
                }finally {
                    lock.unlock();
                }
            }
        });
        Thread thread3 = new Thread(()->{
            for (int i = 0; i < 5; i++) {
                try {
                    lock.lock();
                    System.out.print("c");
                }finally {
                    lock.unlock();
                }
            }
        });
        thread1.start();
        thread2.start();
        thread3.start();
    }

wait/notify版

public static void main(String[] args) {
        WaitNotify waitNotify = new WaitNotify(1,10);
        Thread thread1 = new Thread(()->{
            waitNotify.print("a",1,2);
        });
        Thread thread2 = new Thread(()->{
            waitNotify.print("b",2,3);
        });
        Thread thread3 = new Thread(()->{
            waitNotify.print("c",3,1);
        });
        thread1.start();
        thread2.start();
        thread3.start();
    }
}
class WaitNotify{
    //等待标记
    private int flag;
    //循环次数
    private int loopNumber;

    public WaitNotify(int flag, int loopNumber) {
        this.flag = flag;
        this.loopNumber = loopNumber;
    }

    //打印方法
    public synchronized void print(String content,int waitFlag,int nextFlag){
        for (int i = 0; i < loopNumber; i++) {
            while (flag != waitFlag){
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.print(content);
            flag = nextFlag;
            this.notifyAll();
        }
    }

await/signal

 public static void main(String[] args) throws InterruptedException {
        AwaitSignal awaitSignal = new AwaitSignal(5);
        Condition A = awaitSignal.newCondition();
        Condition B = awaitSignal.newCondition();
        Condition C = awaitSignal.newCondition();
        Thread thread1 = new Thread(()->{
            awaitSignal.print("a",A,B);
        });
        Thread thread2 = new Thread(()->{
            awaitSignal.print("b",B,C);

        });
        Thread thread3 = new Thread(()->{
            awaitSignal.print("c",C,A);
        });
        thread1.start();
        thread2.start();
        thread3.start();
        Thread.sleep(1000);

        try {
            awaitSignal.lock();
            A.signal();
        }finally {
            awaitSignal.unlock();
        }
    }
}
class AwaitSignal extends ReentrantLock{
    private int loopNumber;

    public AwaitSignal(int loopNumber) {
        this.loopNumber = loopNumber;
    }
    public void print(String content,Condition current,Condition next){
        for (int i = 0; i < loopNumber; i++) {
            lock();
            try {
                current.await();
                System.out.print(content);
                next.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                unlock();
            }
        }
    }
}

本章小结

本章我们需要重点掌握的是

  1. 分析多线程访问共享资源时,哪些代码片段属于临界区
  2. 使用 synchronized 互斥解决临界区的线程安全问题
    1. 掌握 synchronized 锁对象语法
    2. 掌握 synchronzied 加载成员方法和静态方法语法
    3. 掌握 wait/notify 同步方法
  3. 使用 lock 互斥解决临界区的线程安全问题 掌握 lock 的使用细节:可打断、锁超时、公平锁、条件变量
  4. 学会分析变量的线程安全性、掌握常见线程安全类的使用
  5. 了解线程活跃性问题:死锁、活锁、饥饿
  6. 应用方面
    1. 互斥:使用 synchronized 或 Lock 达到共享资源互斥效果,实现原子性效果,保证线程安全。
    2. 同步:使用 wait/notify 或 Lock 的条件变量来达到线程间通信效果。
  7. 原理方面
    1. monitor、synchronized 、wait/notify 原理
    2. synchronized 进阶原理
    3. park & unpark 原理
  8. 模式方面
    1. 同步模式之保护性暂停
    2. 异步模式之生产者消费者
    3. 同步模式之顺序控制
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值