JUC Lock synchronized Condition 8锁

1 篇文章 0 订阅
1 篇文章 0 订阅

synchronized 消费者生产者案例

public class ThreadDemo {
    public static void main(String[] args) {
        Resourse resourse = new Resourse();


        new Thread(()->{ try { for (int i = 0; i < 10; i++) { resourse.produce(); Thread.sleep(500);}      } catch (Exception e) { e.printStackTrace(); } }).start();
        new Thread(()->{ try { for (int i = 0; i < 10; i++) { resourse.consumption(); Thread.sleep(1000);     } } catch (Exception e) { e.printStackTrace(); } }).start();
        new Thread(()->{ try { for (int i = 0; i < 10; i++) { resourse.produce();Thread.sleep(500); }      } catch (Exception e) { e.printStackTrace(); } }).start();
        new Thread(()->{ try { for (int i = 0; i < 10; i++) { resourse.consumption();  Thread.sleep(2000)  ;} } catch (Exception e) { e.printStackTrace(); } }).start();
    }

    static class Resourse{
        private Integer num = 0;

        public synchronized void consumption() throws Exception {
            //当没有产品则等待
            while(num==0){  //如果使用if会导致,虚假唤醒  用while 为了在唤醒时 在判断一次看看 资源有没有被改变
               this.wait();
            }
            //等待结束开始消费
            System.out.println(Thread.currentThread().getName()+":当前为"+this.num+"  消费后:"+ --this.num);
            //this.num--;
            //唤醒其他的线程
            this.notifyAll();
        }
        public synchronized void produce() throws Exception {
            //当没有产品则等待
            while(num!=0){
                this.wait();
            }
            //等待结束开始生产
            System.out.println(Thread.currentThread().getName()+":当前为"+this.num+"  生产后:"+ ++this.num);
           // this.num++;
            //唤醒其他的线程
            this.notifyAll();
        }
    }
}

Lock 生产者消费者案例

public class ThreadLock {

    public static void main(String[] args) {
        Resourse resourse = new Resourse();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                resourse.consumer();
            }
        }).start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                resourse.produce();
            }
        }).start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                resourse.consumer();
            }
        }).start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                resourse.produce();
            }
        }).start();
    }
    
    static class Resourse{
        private Integer num = 0;
        private Lock lock = new ReentrantLock();
        private Condition condition = lock.newCondition();


        public void consumer(){
            lock.lock();
            try {
                //当没有产品则等待
                while(num==0){  //如果使用if会导致,虚假唤醒  用while 为了在唤醒时 在判断一次看看 资源有没有被改变
                    condition.await();
                }
                //等待结束开始消费
                System.out.println(Thread.currentThread().getName()+":当前为"+this.num+"  消费后:"+ --this.num);

                //this.num--;
                //唤醒其他的线程
                condition.signalAll();
            }catch (Exception e){

            }finally {
                lock.unlock();
            }
        }

        public void produce(){
            lock.lock();
            try {
                //当没有产品则等待
                while(num!=0){  //如果使用if会导致,虚假唤醒  用while 为了在唤醒时 在判断一次看看 资源有没有被改变
                    condition.await();
                }
                //等待结束开始消费
                System.out.println(Thread.currentThread().getName()+":当前为"+this.num+"  生产后:"+ ++this.num);

                //this.num--;
                //唤醒其他的线程
                condition.signalAll();
            }catch (Exception e){

            }finally {
                lock.unlock();
            }
        }
    }
}

Lock加Condition精准唤醒案例

//线程  操作  资源类
//线程之间的通信    判断   干活   加通知
//标志位
public class ThreadLockCondition {


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

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    resourse.print(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"A").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    resourse.print(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"B").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    resourse.print(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"C").start();
    }


    static class Resourse{
        private Integer number  = 1;
        private Lock lock = new ReentrantLock();
        private Condition condition1 = lock.newCondition();
        private Condition condition2 = lock.newCondition();
        private Condition condition3 = lock.newCondition();

        public void print(int sign) throws InterruptedException {
            if(sign == 1){
                lock.lock();
                try {
                    while(number!=1){
                        //自己睡
                        condition1.await();
                    }
                    for (int i = 0; i < 5; i++) {
                        System.out.println(Thread.currentThread().getName()+":"+i);
                    }
                    number = 2;
                    //唤醒2
                    condition2.signal();
                }catch (Exception e){

                }finally {
                    lock.unlock();
                }

            }else if(sign == 2){
                lock.lock();
                try {
                    while(number!=2){
                        //自己睡
                        condition2.await();
                    }
                    for (int i = 0; i < 10; i++) {
                        System.out.println(Thread.currentThread().getName()+":"+i);
                    }
                    number = 3;
                    //唤醒3
                    condition3.signal();
                }catch (Exception e){

                }finally {
                    lock.unlock();
                }
            }else if(sign == 3){
                lock.lock();
                try {
                    while(number!=3){
                        //自己睡
                        condition3.await();
                    }
                    for (int i = 0; i < 15; i++) {
                        System.out.println(Thread.currentThread().getName()+":"+i);
                    }
                    number = 1;
                    //唤醒1
                    condition1.signal();
                }catch (Exception e){

                }finally {
                    lock.unlock();
                }
            }
        }
    }
}

synchronized 8锁案例加原理

/**
 * 类锁(模板锁),是以.class 做为锁   (类中的静态方法加synchronized 就被此锁锁住)
 * 对象锁   以this做为锁          类中的普通方法加上synchronized就被此锁锁住
 */
public class Lock8 {

    static class Resourse{
        public  synchronized void findAll(){
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("findAll....");
        }

        public static synchronized void save(){
            System.out.println("save....");
        }


        public void hello(){
            System.out.println("say hello");
        }
    }

    public static void main(String[] args) throws Exception {
        Resourse resourse = new Resourse();
        Resourse resourse2 = new Resourse();
        new Thread(()->{
            resourse.findAll();
        }).start();

        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            //resourse.save();
            //resourse.hello();
            resourse2.save();
        }).start();
    }

    /**
     * 1.静态同步findAll   普通同步save   结果  save....   findAll....
     * 2.同步findAll     同步save         结果   findAll....  save....
     * 3.静态同步findAll   普通hello     结果    say hello   findAll....
     * 4.普通同步findAll   普通hello    结果   say hello    findAll....
     * 5.不同对象 普通同步findAll(对象1)   普通同步save(对象2)    结果save....  findAll....
     * 6.不同对象 静态同步findAll(对象1)  静态同步save(对象2)     结果findAll....  save....
     * 7.不同对象 静态同步findAll(对象1)  普通同步save(对象2)    结果save....   findAll....
     * 8.不同对象 普通同步findAll(对象1)  静态同步save(对象2)    结果save....   findAll....
     */

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值