(多线程)ProducerCustomerDemo
来源:互联网 发布:linux查看数据库命令 编辑:程序博客网 时间:2024/06/09 18:52
package duoxiancheng;//多生产者消费者(多条线程输入,多条线程输出)class ResP{private String name;private int count=1 ;private boolean flag=false;public synchronized void set(String name){while(flag)//每次等待都要判断,if会发生不判断直接执行的情况try {this.wait();} catch (InterruptedException e) {// TODO 自动生成的 catch 块e.printStackTrace();}this.name=name+"+++"+count++;System.out.println(Thread.currentThread().getName()+"..."+this.name);this.flag=true;this.notifyAll();//每次唤醒所有线程,防止线程全部等待而冻结}public synchronized void out(){while(!flag)try {this.wait();} catch (InterruptedException e) {// TODO 自动生成的 catch 块e.printStackTrace();}System.out.println(Thread.currentThread().getName()+"...***"+this.name);this.flag=false;this.notifyAll();//注意:此处要加this}}class Producer implements Runnable{ResP r;Producer(ResP r){this.r=r;}public void run(){while(true)r.set("goods");}}class Customer implements Runnable{ResP r;Customer(ResP r){this.r=r;}public void run(){while(true)r.out();}}public class ProducerCustomerDemo {public static void main(String [] args){ResP r=new ResP();new Thread(new Producer(r)).start();new Thread(new Producer(r)).start();new Thread(new Customer(r)).start();new Thread(new Customer(r)).start();}}
0 0
- (多线程)ProducerCustomerDemo
- 多线程(一)多线程基础、多线程状态
- java多线程:多线程(一)
- 多线程(一):初识多线程
- 多线程(多线程面试题)
- Windows多线程技术研究(二):多线程基本概念
- VC多线程编程一(创建多线程)
- 多线程(一)多线程的实现
- C++多线程(二)(_beginThreadex创建多线程)
- 多线程入门教程(五)MFC的多线程
- c# 多线程(一) 多线程的认识
- c# 多线程(二) 多线程的安全
- Java多线程--认识多线程(Thread、Runnable)
- 学习多线程(一)认识多线程
- [java多线程]多线程学习路线图(不断更新)
- Java Tread多线程(2)多线程安全问题
- Java 多线程(五) 多线程的同步
- 多线程编程--多线程间通信(五)
- 遇到的问题汇总
- Android Volley完全解析(三),定制自己的Request
- Maven 创建maven项目
- strong,weak,assign,retain,copy区别与联系
- android常用网址
- (多线程)ProducerCustomerDemo
- 基于android的Socket通信(一)原理
- 支付宝即时到帐接口的python实现,示例采用django框架
- XSS攻击及防御
- mac下xampp环境中yii2的migrate报错处理
- android之socket编程(二)案例
- JAVA反射详解
- 项目查询经验
- c++11 学习及测试(auto,decltype,range for...)