线程通信

线程通信

wait():在其他线程调用此对象的notify()方法或者notifyAll()f方法前导致当前线程等待
notify():唤醒的单条线程
notifyAll():唤醒全部线程

package com.briup.day19;

import javax.swing.table.TableColumn;

/**
 * 生产者和消费者案例
 *
 */
public class ProducterConsumer {

    public static void main(String[] args) {
        Product product = new Product();
        new Thread(new Producer(product)).start();
        new Thread(new Consumer(product)).start();

    }

}

/**
 * 生产者
 *
 */
class Producer implements Runnable {

    private Product product;
    private int x = 0;

    public Producer(Product product) {
        this.product = product;
    }

    @Override
    public void run() {

        while (true) {


            synchronized (product) {
                //生
                if(product.isHave) {
                    //有产品的,则生产者进入等待状态
                    try {
                          //等着,释放锁
                        product.wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }


                if (x % 2 == 0) {
                    // null null
                    product.name = "包子";
                    product.type = "肉";
                } else if (x % 2 == 1) {
                    product.name = "饺子";
                    product.type = "素";
                }

                // 有产品了
                product.isHave=true;     
                //唤醒消费者
                product.notify();
                x++;
            }

        }

    }

}

/**
 * 消费者
 * 
 * @author lixiaojian
 *
 */
class Consumer implements Runnable {

    private Product product;

    public Consumer(Product product) {
        this.product = product;
    }

    @Override
    public void run() {

        while (true) {

            synchronized (product) {

                if (!product.isHave) {
                    // 没产品了,消费者去等待
                    try {
                        // 消
                        product.wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                System.out.println(product.name + "   " + product.type); // 包子 肉
                // 没产品了
                product.isHave = false;
                // 唤醒生产者
                product.notify();
            }

        }

    }

}

/**
 * 产品
 *
 */
class Product {

    String name;
    String type;
    boolean isHave = false;// 一开始是没产品的

}

wait()和sleep()的区别是:sleep()方法不交出锁,相当于停止一定的时间而wait()这个方法是等待,锁交出其他的线程接到锁就可以运行

其实大家看了上面的案例不难发现,线程同步还是缜密的逻辑+同步线程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值