Lock 锁重点(Juc-02)

本贴最后更新于 1245 天前,其中的信息可能已经时移世改

Lock 接口

image.png

image.png

image.png

**公平锁:**十分的公平,可以先来后到

**非公平锁:**十分不公平,可以插队 (默认)

public class SaleTicketDemo02 {
    public static void main(String[] args) {
        Ticket2 ticket = new Ticket2();
        new Thread(()->{for (int i = 0; i <60 ; i++) ticket.sale(); },"A").start();
        new Thread(()->{for (int i = 0; i <60 ; i++) ticket.sale(); },"B").start();
        new Thread(()->{for (int i = 0; i <60 ; i++) ticket.sale(); },"C").start();
    }
}
/*
* 1、new ReentrantLock()
* 2.lock.lock() 加锁
* 3、 finally=>lock.unlock() 解锁
* */
//资源类OOP
class Ticket2 {
    private int number = 50;
    Lock lock = new ReentrantLock();
    public void sale() {
        lock.lock();
        try {
            if(number>0){
                System.out.println(Thread.currentThread().getName()+"买到了第:"+number-- +"票");
            }
        }catch (Exception e){
        }finally {
            lock.unlock();
        }
    }
}

Synchronized 和 Lock 区别

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

生产者和消费者问题 synchronized 版

进行线程之间的通信

下面是两个线程之间的通信

package net.yscxy.pc;

/**
 * @Author WangFuKun
 * @create 2020/11/19 20:46
 */
/*
* 线程之间的通信问题,生产者和消费者问题! 等待唤醒,通知唤醒
* 线程交替执行A B 操作同一个变量 num = 0
* */
public class A {
    public static void main(String[] args) {
        Data data = new Data();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"A").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"B").start();
    }
}
//判断、等待、通知
class Data{
    private int number = 0;
    //+1
    public synchronized void increment() throws InterruptedException {
        if (number!=0){
            this.wait();
        }
        number ++;
        System.out.println(Thread.currentThread().getName()+"->"+number);
        //通知其他线程,我+1完成了
        this.notify();
    }
    //-1
    public synchronized void decrement() throws InterruptedException {
        if(number ==0){
            //等待
            this.wait();
        }
        number --;
        System.out.println(Thread.currentThread().getName()+"->"+number);
        //通知其他线程我-1完成了
        this.notify();
    }
}

问题出现,如果是多个线程的话就会出现问题!,例如纯在 ABCD 四个线程就会出现问题(虚假唤醒!)

image.png

解决方案

if 换成 while,类似下面这样

package net.yscxy.pc;

/**
 * @Author WangFuKun
 * @create 2020/11/19 20:46
 */
/*
* 线程之间的通信问题,生产者和消费者问题! 等待唤醒,通知唤醒
* 线程交替执行A B 操作同一个变量 num = 0
* */
public class A {
    public static void main(String[] args) {
        Data data = new Data();
        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                try {
                    data.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"A").start();
        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                try {
                    data.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"B").start();
        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                try {
                    data.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"C").start();
        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                try {
                    data.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"D").start();
    }
}
//判断、等待、通知
class Data{
    private int number = 0;
    //+1
    public synchronized void increment() throws InterruptedException {
        while (number!=0){
            this.wait();
        }
        number ++;
        System.out.println(Thread.currentThread().getName()+"->"+number);
        //通知其他线程,我+1完成了
        this.notify();
    }
    //-1
    public synchronized void decrement() throws InterruptedException {
        while(number ==0){
            //等待
            this.wait();
        }
        number --;
        System.out.println(Thread.currentThread().getName()+"->"+number);
        //通知其他线程我-1完成了
        this.notify();
    }
}

JUC 版本的生产者和消费者问题

image.png

代码实现

package net.yscxy.pc;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author WangFuKun
 * @create 2020/11/19 20:46
 */
/*
* 线程之间的通信问题,生产者和消费者问题! 等待唤醒,通知唤醒
* 线程交替执行A B 操作同一个变量 num = 0
* */
public class B {
    public static void main(String[] args) {
        Data2 data = new Data2();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"A").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"B").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"C").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"D").start();
    }
}

//判断、等待、通知
class Data2{
    private int number = 0;
    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();
    //+1
    public  void increment() throws InterruptedException {
        lock.lock();
        try {
            //业务代码
            while(number !=0){
                //等待
                condition.await();
            }
            number ++;
            System.out.println(Thread.currentThread().getName()+"->"+number);
            //通知其他线程,我+1完成了
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }
    //-1
    public  void decrement() throws InterruptedException {
        lock.lock();
        try {
            while(number ==0){
                //等待
                condition.await();
            }
            number --;
            System.out.println(Thread.currentThread().getName()+"->"+number);
            condition.signalAll();//唤醒全部
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

image.png

Condition 进准的通知和和唤醒线程

A、B、C、D 有序执行

代码测试

package net.yscxy.pc;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author WangFuKun
 * @create 2020/11/19 20:46
 */
/*
 * 让线程有序执行,A执行完成调用B,B执行完调用C,C执行完调用A
 * */
public class C {
    public static void main(String[] args) {
        Data3 data = new Data3();
        new Thread(() -> {
            for (int i = 0; i <10 ; i++) {
                data.PrintA();
            }
        },"A").start();
        new Thread(() -> {
            for (int i = 0; i <10 ; i++) {
                data.PrintB();
            }
        },"B").start();
        new Thread(() -> {   for (int i = 0; i <10 ; i++) {
            data.PrintC();
        } },"C").start();

    }
}

//判断、等待、通知
class Data3 {
    private int number = 1;
    Lock lock = new ReentrantLock();
    Condition condition1 = lock.newCondition();
    Condition condition2 = lock.newCondition();
    Condition condition3 = lock.newCondition();


    public void PrintA() {
        lock.lock();
        try {
            //业务、判断、执行、通知
            while (number!=1){
                //等待
                condition1.await();
            }
            System.out.println(Thread.currentThread().getName()+"-->AAAAAAAAAA");
            //唤醒指定的人,唤醒2
            number = 2;
            condition2.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }
    public void PrintB() {
        lock.lock();
        try {
            //业务、判断、执行、通知
            while (number!=2){
                //等待
                condition2.await();
            }
            System.out.println(Thread.currentThread().getName()+"-->BBBBBBBBB");
            //唤醒指定的人,唤醒2
            number = 3;
            condition3.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
    public void PrintC() {
        lock.lock();
        try {
            //业务、判断、执行、通知
            while (number!=3){
                //等待
                condition3.await();
            }
            System.out.println(Thread.currentThread().getName()+"-->CCCCCCCCC");
            //唤醒指定的人,唤醒2
            number = 1;
            condition1.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

8 锁现象

如何判断锁的是谁,锁的到底是谁

new this 锁的是具体的一个手机

static Class 唯一的一个模板

  • JUC
    17 引用 • 3 回帖 • 1 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...