线程与进程理论知识入门04-等待唤醒机制(wait与notify)与ThreadLocal

锁.wait() 与 锁.notify()

wait 或者 notify 必须要有锁包裹着

等待区域: wait();
获取对象的锁🔒
synchronized(持有的锁 对象的锁){  //因为被锁住了,你没法获取this锁
    wait();   //内部会释放this锁,这样通知区域才能拿到锁

通知区域: notify();
获取对象的锁🔒
synchronized(持有的锁 对象的锁){
    notify();  //按道理是获取不到锁的 this锁,为什么?

this锁 同一把锁

1.wait 或者 notify 必须要有锁包裹着,因为我们每一步都需要获取锁
2.等待区域已经锁住了为什么通知区域还能拿到锁,因为wait会释放锁

//生产一个消费一个
class Res {
    private int id; //面包的id

    private boolean flag; // 定义标记  默认是false 默认先运行 生产者 后消费

    //生产行为
    public synchronized void put(){
        /**
         * 生产之前判断标记
         */
        if(!flag){
            //开始生产
            id += 1;
            System.out.println(Thread.currentThread().getName()+" 生产者 生产了 :"+this.id);
            //生产完毕
            flag = true;
            /**
             * 唤醒 wait(); 被冻结的线程,
             * 如果没有冻结的线程,没有任何关系,Java默认不会报错,默认会处理的
             */
            notify();//注意: wait 或者 notify  必须要有锁包裹着

            /**
             * 当前自己线程冻结 释放cpu执行权 这个时候cpu就会去执行其他线程了
             */
            try {
                wait();//注意: wait 或者 notify  必须要有锁包裹着
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    //消费行为
    public synchronized void out(){
        /**
         * 消费之前判断标记
         */
        if(flag){
            //开始消费
            System.out.println(Thread.currentThread().getName()+" ============================ 消费者 消费了:"+this.id);
            //消费完成
            flag =false;

            /**
             * 唤醒 wait(); 被冻结的线程,
             * 如果没有冻结的线程,没有任何关系,Java默认不会报错,默认会处理的
             */
            notify();//注意: wait 或者 notify  必须要有锁包裹着

            /**
             * 当前自己线程冻结 释放cpu执行权 这个时候cpu就会去执行其他线程了
             */
            try {
                wait();//注意: wait 或者 notify  必须要有锁包裹着
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}

/**
 * 描述生产者任务
 */
class ProduceRunnable implements Runnable {

    private Res res;

    public ProduceRunnable(Res res) {
        this.res = res;
    }

    @Override
    public void run() {
        for(int i = 0; i < 7; i++){
            res.put();
        }
    }
}

/**
 * 描述消费者任务
 */
class ConsunmeRunnable implements Runnable {

    private Res res;

    public ConsunmeRunnable(Res res) {
        this.res = res;
    }

    @Override
    public void run() {
        for(int i = 0; i < 7; i++){
            res.out();
        }
    }
}


public class ThreadCommunicationDemo {

    public static void main(String[] args) {

        //创建资源对象
        Res res = new Res();

        //创建生产者任务
        ProduceRunnable produceRunable = new ProduceRunnable(res);

        //创建消费者任务
        ConsunmeRunnable consumeRunable = new ConsunmeRunnable(res);

        //启动生产者任务
        new Thread(produceRunable).start();

        //启动消费者任务
        new Thread(consumeRunable).start();
    }
}
Thread-0 生产者 生产了 :1
Thread-1 ============================ 消费者 消费了:1
Thread-0 生产者 生产了 :2
Thread-1 ============================ 消费者 消费了:2
Thread-0 生产者 生产了 :3
Thread-1 ============================ 消费者 消费了:3
Thread-0 生产者 生产了 :4
Thread-1 ============================ 消费者 消费了:4
Thread-0 生产者 生产了 :5
Thread-1 ============================ 消费者 消费了:5
Thread-0 生产者 生产了 :6
Thread-1 ============================ 消费者 消费了:6
Thread-0 生产者 生产了 :7
Thread-1 ============================ 消费者 消费了:7

锁.notify()与锁.notifyAll()

锁.notify(): 不确定性唤醒某一个等待中的线程
锁.notifyAll(): 唤醒所有等待中的线程,尽量使用notifyAll()唤醒所有等待中的线程

ThreadLocal

/**
 * ThreadLocal 线程的隔离
 * Handler 里面用到了 ThreadLocal
 */
public class TestThreadLocal {

    static ThreadLocal<String> threadLocal = new ThreadLocal<String>(){

        @Override
        protected String initialValue() {
            return "ThreadLocal";
        }
    };

    // TODO 线程一
    private static class StudentThread extends Thread{

        @Override
        public void run() {
            super.run();
            //理解:copy 副本 set 是操作我的副本

            String threadName = currentThread().getName();

            System.out.println("首次拿 threadName:"+ threadName+" get: "+ threadLocal.get()); //ThreadLocal

            threadLocal.set("StudentThread"); //我这里修改的 是在StudentThread 修改的,和其他线程没有半毛钱关系

            System.out.println("threadName:"+ threadName+" get: "+ threadLocal.get()); //StudentThread

            //线程一 set A  我在线程一 所获取的所有内容 都是A
            //不可能拿到其他线程设置的值

            //get先拿到 当前线程 StudentThread Map(Key==当前线程) 的值
            //set 先拿到 当前线程 StudentThread Map(Key==当前线程) 给当前线程唯一的 设置 A

        }
    }

    // TODO 线程二
    private static class PersonThread extends Thread {

        @Override
        public void run() {
            super.run();
            //理解:copy 副本 set 是操作我的副本

            String threadName = currentThread().getName();

            System.out.println("首次拿 threadName:"+ threadName+" get: "+ threadLocal.get()); //ThreadLocal

            threadLocal.set("PersonThread"); //我这里修改的 PersonThread 修改的,和其他线程没有半毛钱关系

            System.out.println("threadName:"+ threadName+" get: "+ threadLocal.get()); //PersonThread
        }
    }

    public static void main(String[] args) {

        new StudentThread().start();
        new PersonThread().start();

        //Main 线程
        System.out.println("Main线程 :"+ threadLocal.get());
    }

}
首次拿 threadName:Thread-0 get: ThreadLocal
threadName:Thread-0 get: StudentThread
Main线程 :ThreadLocal
首次拿 threadName:Thread-1 get: ThreadLocal
threadName:Thread-1 get: PersonThread
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值