目录
- 1.等待唤醒机制
- 1.ThreadDemo
- 2.Desk
- 3.Cook
- 4.Foodie
 
- 2.等待唤醒机制(阻塞队列方式实现)
- 1.ThreadDemo02
- 2.Cook02
- 3.Foodie02
 
- 3.线程的状态
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
- //先判断桌子上是否有面条
- //有面条,线程等待
- //没有面条,厨师开始做
- //修改桌子上的食物状态
Desk.foodFlag=1;- //做完之后唤醒消费者开吃
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
- //先判断桌子上是否有面条
- //没有面条,线程等待
- //有面条,吃货开始吃
- //吃的总数减一
Desk.count–;- //吃完之后唤醒厨师继续做
Desk.lock.notifyAll();- //修改桌子的状态
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
- 利用阻塞队列完成生产者和消费者等待唤醒机制的代码
- 生产者和消费者必须使用同一个阻塞队列
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()就绪状态后交给操作系统的





















