线程 --生产和消费问题
2011-08-09 15:35
295 查看
package com.lan;
/**
* 生产和消费的问题
* @ClassName: ProducerConsumer
* @Description: TODO
* @author LanHantong
* @date 2011-8-9 下午03:13:39
*
*/
public class ProducerConsumer {
/**
* @param args
*/
public static void main(String[] args) {
SyncStack ss = new SyncStack();
Producer p = new Producer(ss);
Consumer c = new Consumer(ss);
new Thread(p).start();
new Thread(c).start();
}
}
//窝头类
class WoTou{
int id;
WoTou(int id){
this.id = id;
}
public String toString(){
return "wotou :" + id;
}
}
//箩筐类
class SyncStack{
int index = 0;
WoTou[] arrWT = new WoTou[6];
//生产方法
public synchronized void push(WoTou wt){
while(index == arrWT.length ){
try {
//等待
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//唤醒其他线程
this.notify();
arrWT[index] = wt;
index++;
}
//消费方法
public synchronized WoTou pop(){
while(index == 0){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notify();
index--;
return arrWT[index];
}
}
//生产线程
class Producer implements Runnable{
SyncStack ss = null;
//构造方法 持有SyncStack的引用
Producer(SyncStack ss){
this.ss = ss;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0; i<20; i++){
WoTou wt = new WoTou(i);
ss.push(wt);
System.out.println("生产了,"+wt);
try {
Thread.sleep((int)(Math.random()*200));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//消费线程
class Consumer implements Runnable{
SyncStack ss = null;
//构造方法 持有SyncStack的引用
Consumer(SyncStack ss){
this.ss = ss;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0; i<20; i++){
WoTou wt = ss.pop();
System.out.println("消费了,"+wt);
try {
Thread.sleep((int)(Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* 生产和消费的问题
* @ClassName: ProducerConsumer
* @Description: TODO
* @author LanHantong
* @date 2011-8-9 下午03:13:39
*
*/
public class ProducerConsumer {
/**
* @param args
*/
public static void main(String[] args) {
SyncStack ss = new SyncStack();
Producer p = new Producer(ss);
Consumer c = new Consumer(ss);
new Thread(p).start();
new Thread(c).start();
}
}
//窝头类
class WoTou{
int id;
WoTou(int id){
this.id = id;
}
public String toString(){
return "wotou :" + id;
}
}
//箩筐类
class SyncStack{
int index = 0;
WoTou[] arrWT = new WoTou[6];
//生产方法
public synchronized void push(WoTou wt){
while(index == arrWT.length ){
try {
//等待
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//唤醒其他线程
this.notify();
arrWT[index] = wt;
index++;
}
//消费方法
public synchronized WoTou pop(){
while(index == 0){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notify();
index--;
return arrWT[index];
}
}
//生产线程
class Producer implements Runnable{
SyncStack ss = null;
//构造方法 持有SyncStack的引用
Producer(SyncStack ss){
this.ss = ss;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0; i<20; i++){
WoTou wt = new WoTou(i);
ss.push(wt);
System.out.println("生产了,"+wt);
try {
Thread.sleep((int)(Math.random()*200));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//消费线程
class Consumer implements Runnable{
SyncStack ss = null;
//构造方法 持有SyncStack的引用
Consumer(SyncStack ss){
this.ss = ss;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0; i<20; i++){
WoTou wt = ss.pop();
System.out.println("消费了,"+wt);
try {
Thread.sleep((int)(Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
相关文章推荐
- 线程---生产消费问题
- 多线程经典问题:馒头问题 生产和消费问题(线程的通信问题)
- java学习9:生产和消费问题,先生产才能消费,线程间通信与等待
- 在并发编程中使用生产者和消费者模式能够解决绝大多数并发问题。该模式通过平衡生产线程和消费线程的工作能力来提高程序的整体处理数据的速度。
- java生产消费线程小例子
- Java多线程的单生产单消费和多生产多消费问题的解决
- synchronized/wait/notify 与 mutex/cond wait wake ~ 链表队列 生产消费问题
- Java学习札记之线程模型——生产消费模型
- 两个线程执行生产 * 两线程执行消费 * 依次交替循环
- 线程间通信:生产线程和消费线程
- 第三周第一天(【正则表达式的应用】【卖票问题】【同一个账号两人同时取钱】【生产消费问题】【join】【死锁问题】)
- 线程生产消费模式
- 生产消费线程demo
- 生产消费线程讲解例子
- Java模拟生产消费问题
- Java 多线程间通信 多生产 多消费问题
- 多线程中的多生产多消费问题
- 多线程之偷了个懒之经典生产消费问题
- 使用Lock与ReentrantLock模拟消息队列阻塞,生产与消费问题模拟
- 【Java】----线程同步:生产-消费问题