zoukankan      html  css  js  c++  java
  • Lock锁

    Lock锁

    1. Synchronized
    @SuppressWarnings("all")
    public class SaleTicketDemo01 {
        public static void main(String[] args) {
            // 并发:多线程操作同一个资源类, 把资源类丢入线程
            Ticket ticket = new Ticket();
    
            // @FunctionalInterface 函数式接口,jdk1.8  lambda表达式 (参数)->{ 代码 }
            new Thread(()->{
                for (int i = 1; i < 40 ; i++) {
                    ticket.sale();
                }
            },"A").start();
    
            new Thread(()->{
                for (int i = 1; i < 40 ; i++) {
                    ticket.sale();
                }
            },"B").start();
    
            new Thread(()->{
                for (int i = 1; i < 40 ; i++) {
                    ticket.sale();
                }
            },"C").start();
    
    
        }
    }
    
    // 资源类 OOP
    class Ticket {
        // 属性、方法
        private int number = 30;
    
        // 卖票的方式
        // synchronized 本质: 队列,锁
        public synchronized void sale(){
            if (number>0){
                System.out.println(Thread.currentThread().getName()+"卖出了"+(number--)+"票,剩余:"+number);
            }
        }
    
    }
    
    1. Lock锁
    public class SaleTicketDemo02  {
        public static void main(String[] args) {
    
            // 并发:多线程操作同一个资源类, 把资源类丢入线程
            Ticket2 ticket = new Ticket2();
    
    
            // @FunctionalInterface 函数式接口,jdk1.8  lambda表达式 (参数)->{ 代码 }
            new Thread(()->{for (int i = 1; i < 40 ; i++) ticket.sale();},"A").start();
            new Thread(()->{for (int i = 1; i < 40 ; i++) ticket.sale();},"B").start();
            new Thread(()->{for (int i = 1; i < 40 ; i++) ticket.sale();},"C").start();
    
    
        }
    }
    
    // Lock三部曲
    // 1、 new ReentrantLock();
    // 2、 lock.lock(); // 加锁
    // 3、 finally=>  lock.unlock(); // 解锁
    class Ticket2 {
        // 属性、方法
        private int number = 30;
    
        Lock lock = new ReentrantLock();
    
        public void sale(){
    
            lock.lock(); // 加锁
    
            try {
               // 业务代码
    
                if (number>0){
                    System.out.println(Thread.currentThread().getName()+"卖出了"+(number--)+"票,剩余:"+number);
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock(); // 解锁
            }
        }
    
    }
    

    interface Lock

    image-20200802170817522

    /**
     * Creates an instance of {@code ReentrantLock}.
     * This is equivalent to using {@code ReentrantLock(false)}.
     */
    public ReentrantLock() {
        //非公平锁
        sync = new NonfairSync();
    }
    
    /**
     * Creates an instance of {@code ReentrantLock} with the
     * given fairness policy.
     *
     * @param fair {@code true} if this lock should use a fair ordering policy
     */
    public ReentrantLock(boolean fair) {
        //FairSync 公平锁,线程先到先执行
        sync = fair ? new FairSync() : new NonfairSync();
    }
    

    Synchronized 和 Lock 区别

    1. Synchronized 内置的Java关键字, Lock 是一个Java类
    2. Synchronized 无法判断获取锁的状态,Lock 可以判断是否获取到了锁
    3. Synchronized 会自动释放锁,lock 必须要手动释放锁!如果不释放锁,死锁
    4. Synchronized 线程 1(获得锁,阻塞)、线程2(等待,傻傻的等);Lock锁就不一定会等待下 去;
    5. Synchronized 可重入锁,不可以中断的,非公平;Lock ,可重入锁,可以 判断锁,非公平(可以 自己设置);
    6. Synchronized 适合锁少量的代码同步问题,Lock 适合锁大量的同步代码!

    视频参考https://www.bilibili.com/video/BV1B7411L7tE
    上一篇:线程和进程
    下一篇:生产者消费者问题

  • 相关阅读:
    LintCode Python 简单级题目 488.快乐数
    LintCode Python 简单级题目 100.删除排序数组中的重复数字 101.删除排序数组中的重复数字II
    LintCode Python 简单级题目 373.奇偶分割数组
    LintCode Python 简单级题目 39.恢复旋转排序数组
    LintCode Python 简单级题目 35.翻转链表
    LintCode Python 简单级题目 451.两两交换链表中的节点
    LintCode Python 简单级题目 174.删除链表中倒数第n个节点
    aws查看官方centos镜像imageid
    linux shell脚本查找重复行/查找非重复行/去除重复行/重复行统计
    php配置优化-生产环境应用版
  • 原文地址:https://www.cnblogs.com/junlinsky/p/13443191.html
Copyright © 2011-2022 走看看