多线程进阶(JUC-1)准备知识

多线程基础见基础1,其余可以查看我的专栏多线程

  1. 查看CPU逻辑处理器数
    System.out.println(Runtime.getRuntime().availableProcessors());
  2. Java多线程的状态
    源码如下
    public enum State {
        // 新生状态
        NEW,
        // 运行状态
        RUNNABLE,
		// 阻塞状态
        BLOCKED,
		// 等待状态(一直等待)
        WAITING,
		// 超时等待
        TIMED_WAITING,
        // 终止
        TERMINATED;
    }
  1. wait/sleep的区别
  • 来自不同的类
    wait => Object
    sleep =>Thread
  • 关于锁的释放
    wait会释放锁,sleep不会释放锁
  • 使用范围不同
    wait:必须在同步代码块中使用
    sleep:什么时候都可以用
  1. 通知其他线程准备唤醒
    this.notifyAll();
  2. 线程创建的方式
    1. 继承Thread类创建线程
    2. 实现Runnable接口创建线程

Lock锁

  • 传统的锁Synchronized
    public synchronized void test()在方法上加入synchronized
  • lock
    源码
   public ReentrantLock() {
        sync = new NonfairSync(); //NonfairSync() 非公平锁  FairSync() 公平锁
    }
    public ReentrantLock(boolean fair) {
        sync = fair ? new FairSync() : new NonfairSync();
    }

公平锁:先来先服务
非公平锁(默认): 可插队

Lock lock = new ReentrantLock();
        lock.lock();
        // 尝试获取锁
        lock.tryLock();
        try {

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }

Synchronized和Lock区别

  1. synchronized 是Java内部的关键字,Lock 是一个Java类
  2. synchronized 无法判断锁的状态,Lock 可以判断是否取到了锁
  3. synchronized 是自动释放锁,Lock需要手动释放锁(如果不释放,产生死锁)
  4. synchronized 当一个线程获得锁,如果线程被阻塞;另一个线程只能一直去等待,Lock锁不一定一直等待
  5. synchronized 可重入锁、不可中断锁、非公平锁,Lock 可重入锁、可以判断锁,非公平(默认非公平,可以设置公平锁)
  6. synchronized 适用于少量代码的同步问题,Lock 适用于锁大量的同步代码

什么是锁,怎么判断锁的是谁?

  1. ​ 在计算机科学中,锁(lock)与互斥(mutex)是一种同步机制,用于在许多线程执行时对资源的限制。
    ​ 锁通常需要硬件支持才可以有效实施。这种支持通常采用一个或多个原子指令,测试单个线程是否空闲。

synchrnoized实现传统的生产者消费者模式

package demo1.Juc;
public class demo1 {
    private int data = 0;
    public static void main(String[] args) {
        demo1 demo = new demo1();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    demo.inCr();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"A").start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    demo.deCr();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"B").start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    demo.inCr();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"C").start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    demo.deCr();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"D").start();
    }


    public synchronized void inCr() throws InterruptedException {
        if (data!=0){
            this.wait();
        }
        data++;
        System.out.println(Thread.currentThread().getName()+"=>"+data);
        this.notifyAll();
    }
    public synchronized void deCr() throws InterruptedException {
        if (data==0){
            this.wait();
        }
        data--;
        System.out.println(Thread.currentThread().getName()+"=>"+data);
        this.notifyAll();
    }
}

问题:虚假唤醒
虚假唤醒:一般而言线程调用wait()方法后,需要其他线程调用notify,notifyAll方法后,线程才会从wait方法中返回, 而虚假唤醒(spurious wakeup)是指线程通过其他方式,从wait方法中返回。
在这里插入图片描述
在这里插入图片描述
解决:将if判断改为while判断

package demo1.Juc;
public class demo1 {
    private int data = 0;
    public static void main(String[] args) {
        demo1 demo = new demo1();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    demo.inCr();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"A").start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    demo.deCr();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"B").start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    demo.inCr();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"C").start();
        
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    demo.deCr();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"D").start();
    }
    public synchronized void inCr() throws InterruptedException {
        while (data!=0){
            this.wait();
        }
        data++;
        System.out.println(Thread.currentThread().getName()+"=>"+data);
        this.notifyAll();
    }
    public synchronized void deCr() throws InterruptedException {
        while (data==0){
            this.wait();
        }
        data--;
        System.out.println(Thread.currentThread().getName()+"=>"+data);
        this.notifyAll();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啊~小 l i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值