java多线程之 生产者和消费者 线程间通信 等待与唤醒机制

生产者和消费者 线程间通信  等待与唤醒机制

  必须生产者生产,消费者才能消费

  生产者生产一个 ,消费者才能消费,否则等待,等待消费者消费一个后去唤醒
  消费者消费一个,生产者才能生产,否则等待,等待生产者生产一个后去唤醒
  Oject类中3个方法:
  1.notify() 唤醒单个线程
  2.notifyAll()唤醒所有线程
  3.wait() 等待线程

 注意 同步唤醒机制在同步锁内进行,先判断等待wait() ,然后执行多线程共同操作的内容,最后唤醒notify()

1.测试类

package ProductorAndConsumer;
/**
 * 测试类
 * @author feige
 */
public class StudentDemo {

    public static void main(String[] args) {
        Student s=new Student();
        StudentProductor sp=new StudentProductor(s);
        StudentConsumer sc=new StudentConsumer(s);
        Thread spt=new Thread(sp);
        Thread sct=new Thread(sc);
        spt.start();
        sct.start();
    }
}
2.student类

package ProductorAndConsumer;

/**
 * 学生类
 * @author feige
 */
public class Student {
    private String name;
    private int age;
    boolean flag;

    public synchronized void set(String name, int age) {
        while (this.flag) {// 如果有货,就等待消费者消费
            try {
                this.wait();// 先判断等待,等唤醒后从这里开始
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.name = name;
        this.age = age;

        System.out.println("生产者:name="+this.name+",年龄="+this.age);

        this.flag = true;
        this.notify();// 生产完后,解锁,去通知消费者,但此时消费者不能保证立马获得cpu执行权限,只能去竞争资源
    }

    public synchronized void get() {
        while (!this.flag) {// 如果没货,就等待生产者生产
            try {
                this.wait();// 先判断等待,等唤醒后从这里开始
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("消费者:name=" + this.name + ",年龄=" + this.age);
        this.flag = false;
        this.notify();// 消费完一个后,就通知生产者,同样不能保证生产者立马获得cpu执行权限,只能去竞争资源
    }
}

3.StudentProductor类 生产者

package ProductorAndConsumer;
/**
 * 生产者
 * @author feige
 */
public class StudentProductor implements Runnable {
    private Student s;
    private int x=1;
    
    public StudentProductor(Student s) {
        this.s=s;
    }
    @Override
    public void run() {
        while(true){
                   if(x%2==0){
                        s.set("阿飞",25);
                    }else{
                        s.set("阿瑞",24);
                    }
                   x++;
            }    
        }
    }
4. StudentConsumer类  消费者

package ProductorAndConsumer;
/**
 * 消费者
 * @author feige
 */
public class StudentConsumer implements Runnable{
    private Student s;
    
    public StudentConsumer(Student s) {
        this.s=s;
    }

    @Override
    public void run() {
        while(true){
            s.get();
            }
        }
    }
运行结果可以看到 生产者和消费者交替进行


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值