Java多线程03——等待唤醒机制(and阻塞队列实现)

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

目录

1.等待唤醒机制

生产者和消费者
桌子上有食物消费者吃桌子上没有食物消费者等待唤醒生产者生产者准备食物。生产者准备好食物唤醒消费者消费者开吃
在这里插入图片描述

1.ThreadDemo

package threadwaitandnotify;

/**
 * @version 1.0
 * @auther Demo龙
 */
public class ThreadDemo {
    public static void main(String[] args) {
        //创建线程的对象
        Cook c=new Cook();
        Foodie f=new Foodie();
        c.setName("厨师");
        f.setName("吃货");
        c.start();
        f.start();
    }
}

2.Desk

作用控制生产者和消费者的执行
是否有面条 foodFlag=0没有 。 foodFlag=1有

package threadwaitandnotify;

/**
 * @version 1.0
 * @auther Demo龙
 */
public class Desk {

    /*
    * 作用控制生产者和消费者的执行
    * */
    //是否有面条 0没有  1有
    public static int foodFlag=0;

    //总个数
    public static int count=10;

    //锁对象
    public static Object lock=new Object();
}

3.Cook

  1. //先判断桌子上是否有面条
  2. //有面条线程等待
  3. //没有面条,厨师开始做
  4. //修改桌子上的食物状态
    Desk.foodFlag=1;
  5. //做完之后唤醒消费者开吃
package threadwaitandnotify;

/**
 * @version 1.0
 * @auther Demo龙
 */
public class Cook extends Thread{
    @Override
    public void run() {
        while (true){
            synchronized (Desk.lock){
                if(Desk.count==0){
                    break;
                }else {
                    //先判断桌子上是否有面条
                    if(Desk.foodFlag==1){
                        //有面条线程等待
                        try {
                            Desk.lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }else {
                        //没有面条,厨师开始做
                        System.out.println(Thread.currentThread().getName()+"做了一碗面条");
                        //修改桌子上的食物状态
                        Desk.foodFlag=1;
                        //吃完之后唤醒消费者开吃
                        Desk.lock.notifyAll();
                    }
                }
            }
        }
    }
}

4.Foodie

  1. //先判断桌子上是否有面条
  2. //没有面条线程等待
  3. //有面条,吃货开始吃
  4. //吃的总数减一
    Desk.count–;
  5. //吃完之后唤醒厨师继续做
    Desk.lock.notifyAll();
  6. //修改桌子的状态
    Desk.foodFlag=0;
package threadwaitandnotify;

/**
 * @version 1.0
 * @auther Demo龙
 */
public class Foodie extends Thread{

    @Override
    public void run() {
        while (true){
            synchronized (Desk.lock){
                if(Desk.count==0){
                    break;
                }else {
                    //先判断桌子上是否有面条
                    if(Desk.foodFlag==0){
                        //没有线程等待
                        try {
                            Desk.lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }else {
                        //有面条,开吃
                        //吃的总数减一
                        Desk.count--;
                        System.out.println(Thread.currentThread().getName()+"在吃面条还能再吃"+Desk.count+"碗");
                        //吃完之后唤醒厨师继续做
                        Desk.lock.notifyAll();
                        //修改桌子的状态
                        Desk.foodFlag=0;
                    }
                }
            }
        }
    }
}

在这里插入图片描述

2.等待唤醒机制阻塞队列方式实现

1.ThreadDemo02

  1. 利用阻塞队列完成生产者和消费者等待唤醒机制的代码
  2. 生产者和消费者必须使用同一个阻塞队列
package threadwaitandnotify02;

import java.util.concurrent.ArrayBlockingQueue;

/**
 * @version 1.0
 * @auther Demo龙
 */
public class ThreadDemo02 {
    //等待唤醒机制阻塞队列方式实现
    public static void main(String[] args) {
        /*
        * 1.利用阻塞队列完成生产者和消费者等待唤醒机制的代码
        * 2.生产者和消费者必须使用同一个阻塞队列
        * */
        //创建阻塞队列
        ArrayBlockingQueue <String>queue=new ArrayBlockingQueue<>(1);
        //创建线程的对象
        Cook02 c=new Cook02(queue);
        Foodie02 f=new Foodie02(queue);
        c.start();
        f.start();
    }
}

2.Cook02

package threadwaitandnotify02;



import java.util.concurrent.ArrayBlockingQueue;

/**
 * @version 1.0
 * @auther Demo龙
 */
public class Cook02 extends Thread{
    //成员变量阻塞队列
    ArrayBlockingQueue<String> queue;
    public Cook02(ArrayBlockingQueue<String> queue){//构造方法赋值
        this.queue=queue;
    }
    @Override
    public void run() {
        while (true){
            try {
                queue.put("面包");
                System.out.println("厨师做了一个面包");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

3.Foodie02

package threadwaitandnotify02;

import java.util.concurrent.ArrayBlockingQueue;

/**
 * @version 1.0
 * @auther Demo龙
 */
public class Foodie02 extends Thread{
    //成员变量阻塞队列
    ArrayBlockingQueue<String> queue;
    public Foodie02(ArrayBlockingQueue<String> queue){//构造方法赋值
        this.queue=queue;
    }
    @Override
    public void run() {
        while (true){
            try {
                String q=queue.take();
                System.out.println(""+q);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

在这里插入图片描述

3.线程的状态

在这里插入图片描述

没有运行状态运行状态是Java运行start()就绪状态后交给操作系统的

在这里插入图片描述

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Java