使用点餐的过程示例线程交互

来源:互联网 发布:mssql数据库备份分离 编辑:程序博客网 时间:2024/05/22 12:34

好记性不如赖笔头…………

注意一点:在唤醒线程时,必须使用锁对象唤醒,否则,会报错。在进行wait状态时,锁并没有让出去。

这里主要讲述的是线程的交互

点餐的实体类:

package com.Ckinghan.threadEach;/** * @author Ckinghan *  @描述:线程交互,点餐实体类 */public class Rice {    //点的菜名    private String name;    //点的菜是否已经做好了    private boolean isNo;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public boolean isNo() {        return isNo;    }    public void setNo(boolean isNo) {        this.isNo = isNo;    }}

做饭的类:

package com.Ckinghan.threadEach;/** * @author Ckinghan *  @描述:线程交互 */public class Cook implements Runnable {    //点餐的内容     private Rice rice;    //构造参数    public Cook(Rice rice) {        this.rice = rice;    }    @Override    public void run() {        while (true) {            //加锁            synchronized (rice) {                //如果叫的餐已经有做好的                if(rice.isNo()){                    try {                        //等待下一个叫餐菜单                        rice.wait();                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }else{                    try {                        //休息1秒钟发个呆                        Thread.sleep(1000);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    //做饭                    System.out.println("正在做饭!");                    //饭做好了,告诉服务员                    rice.setNo(true);                    //叫醒一个线程去送饭去                    rice.notify();                }            }        }    }}

吃饭的类:

package com.Ckinghan.threadEach;/** * @author Ckinghan *  @描述:线程交互 */public class Eat implements Runnable {    /**     * 点餐对象     */    private Rice rice;    /**     * 构造参数必须输入点的餐     */    public Eat(Rice rice) {        this.rice = rice;    }    /**     * @描述:     * @创建时间:     */    @Override    public void run() {        /**         * 一直接执行下去         */        while(true){            /**             * 锁定rice对象             */            synchronized (rice) {                //如果饭还没做好                if(!rice.isNo()){                    try {                        //等待                        rice.wait();                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }else{                    try {                        //休息1秒等待饭上桌,再去吃饭                        Thread.sleep(1000);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    //吃饭                    System.out.println("正在吃饭!");                    //吃完了,设置为饭没有了                    rice.setNo(false);                    //叫醒一个等待的线程起床去干活                    rice.notify();                }            }        }    }}

用户点餐的测试类:

package com.Ckinghan.threadEach;/** * @author Ckinghan *  @描述:线程交互 */public class Demo {    public static void main(String[] args) {        //客人点菜了        Rice rice = new Rice();        rice.setName("青椒炒蛋");        //当前的菜没有做        rice.setNo(false);        //做饭行为        Cook cook = new Cook(rice);        //吃饭行为        Eat eat = new Eat(rice);        /**         * 创建两个线程,一个去做饭,一个去吃饭         */        Thread thread1 = new Thread(eat);        Thread thread2 = new Thread(cook);        /**         * 让两个线程去干活         */        thread1.start();        thread2.start();    }}

执行结果:

正在做饭!正在吃饭!正在做饭!正在吃饭!正在做饭!正在吃饭!正在做饭!正在吃饭!正在做饭!