zoukankan      html  css  js  c++  java
  • JUC基础(六):Lock 同步锁

    显示锁 Lock

     在 Java 5.0 之前,协调共享对象的访问时可以使用的机制只有 synchronized 和 volatile 。Java 5.0 后增加了一些新的机制,但并不是一种替代内置锁的方法,而是当内置锁不适用时,作为一种可选择的高级功能。
     ReentrantLock 实现了 Lock 接口,并提供了与synchronized 相同的互斥性和内存可见性。但相较于synchronized 提供了更高的处理锁的灵活性。
    package com.atguigu.juc;
    
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    /*
     * 一、用于解决多线程安全问题的方式:
     * 
     * synchronized:隐式锁
     * 1. 同步代码块
     * 
     * 2. 同步方法
     * 
     * jdk 1.5 后:
     * 3. 同步锁 Lock
     * 注意:是一个显示锁,需要通过 lock() 方法上锁,必须通过 unlock() 方法进行释放锁
     */
    public class TestLock {
        
        public static void main(String[] args) {
            Ticket ticket = new Ticket();
            
            new Thread(ticket, "1号窗口").start();
            new Thread(ticket, "2号窗口").start();
            new Thread(ticket, "3号窗口").start();
        }
    
    }
    
    class Ticket implements Runnable{
        
        private int tick = 100;
        
        private Lock lock = new ReentrantLock();
    
        @Override
        public void run() {
            while(true){
                
                lock.lock(); //上锁
                
                try{
                    if(tick > 0){
                        try {
                            Thread.sleep(200);
                        } catch (InterruptedException e) {
                        }
                        
                        System.out.println(Thread.currentThread().getName() + " 完成售票,余票为:" + --tick);
                    }
                }finally{
                    lock.unlock(); //释放锁
                }
            }
        }
        
    }

     案例二:

    package com.atguigu.juc;
    
    /*
     * 生产者和消费者案例
     */
    public class TestProductorAndConsumer {
    
        public static void main(String[] args) {
            Clerk clerk = new Clerk();
            
            Productor pro = new Productor(clerk);
            Consumer cus = new Consumer(clerk);
            
            new Thread(pro, "生产者 A").start();
            new Thread(cus, "消费者 B").start();
            
            new Thread(pro, "生产者 C").start();
            new Thread(cus, "消费者 D").start();
        }
        
    }
    
    /*//店员
    class Clerk{
        private int product = 0;
        
        //进货
        public synchronized void get(){//循环次数:0
            while(product >= 1){//为了避免虚假唤醒问题,应该总是使用在循环中
                System.out.println("产品已满!");
                
                try {
                    this.wait();
                } catch (InterruptedException e) {
                }
                
            }
            
            System.out.println(Thread.currentThread().getName() + " : " + ++product);
            this.notifyAll();
        }
        
        //卖货
        public synchronized void sale(){//product = 0; 循环次数:0
            while(product <= 0){
                System.out.println("缺货!");
                
                try {
                    this.wait();
                } catch (InterruptedException e) {
                }
            }
            
            System.out.println(Thread.currentThread().getName() + " : " + --product);
            this.notifyAll();
        }
    }
    
    //生产者
    class Productor implements Runnable{
        private Clerk clerk;
    
        public Productor(Clerk clerk) {
            this.clerk = clerk;
        }
    
        @Override
        public void run() {
            for (int i = 0; i < 20; i++) {
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                }
                
                clerk.get();
            }
        }
    }
    
    //消费者
    class Consumer implements Runnable{
        private Clerk clerk;
    
        public Consumer(Clerk clerk) {
            this.clerk = clerk;
        }
    
        @Override
        public void run() {
            for (int i = 0; i < 20; i++) {
                clerk.sale();
            }
        }
    }*/
    package com.atguigu.juc;
    
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    /*
     * 生产者消费者案例:
     */
    public class TestProductorAndConsumerForLock {
    
        public static void main(String[] args) {
            Clerk clerk = new Clerk();
    
            Productor pro = new Productor(clerk);
            Consumer con = new Consumer(clerk);
    
            new Thread(pro, "生产者 A").start();
            new Thread(con, "消费者 B").start();
    
    //         new Thread(pro, "生产者 C").start();
    //         new Thread(con, "消费者 D").start();
        }
    
    }
    
    class Clerk {
        private int product = 0;
    
        private Lock lock = new ReentrantLock();
        private Condition condition = lock.newCondition();
    
        // 进货
        public void get() {
            lock.lock();
    
            try {
                if (product >= 1) { // 为了避免虚假唤醒,应该总是使用在循环中。
                    System.out.println("产品已满!");
    
                    try {
                        condition.await();
                    } catch (InterruptedException e) {
                    }
    
                }
                System.out.println(Thread.currentThread().getName() + " : "
                        + ++product);
    
                condition.signalAll();
            } finally {
                lock.unlock();
            }
    
        }
    
        // 卖货
        public void sale() {
            lock.lock();
    
            try {
                if (product <= 0) {
                    System.out.println("缺货!");
    
                    try {
                        condition.await();
                    } catch (InterruptedException e) {
                    }
                }
    
                System.out.println(Thread.currentThread().getName() + " : "
                        + --product);
    
                condition.signalAll();
    
            } finally {
                lock.unlock();
            }
        }
    }
    
    // 生产者
    class Productor implements Runnable {
    
        private Clerk clerk;
    
        public Productor(Clerk clerk) {
            this.clerk = clerk;
        }
    
        @Override
        public void run() {
            for (int i = 0; i < 20; i++) {
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
                clerk.get();
            }
        }
    }
    
    // 消费者
    class Consumer implements Runnable {
    
        private Clerk clerk;
    
        public Consumer(Clerk clerk) {
            this.clerk = clerk;
        }
    
        @Override
        public void run() {
            for (int i = 0; i < 20; i++) {
                clerk.sale();
            }
        }
    
    }
  • 相关阅读:
    C++ 虚函数表解析
    函数调用运算符重载
    成员访问运算符重载
    递增和递减运算符重载
    java 如何查看jdk版本&位数
    oracle 11g完美卸载
    win10 你没有足够的权限执行此操作。
    maven 找不到或无法加载主类
    eclipse 如何对maven项目进行打包?
    Core Dataeasy出现的错误
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/14238435.html
Copyright © 2011-2022 走看看