Thread--不同步的生产者与消费者
2011-07-08 19:18
169 查看
/**
*
* 共享缓冲区的接口
*
*/
public interface Buffer {
//生产者使用的
public abstract void set(int value);
//消费者使用的
public abstract int get();
}
/**
*
* 消费者
*
*/
public class Consumer extends Thread{
/*缓冲区*/
private Buffer sharedLocation;
public Consumer( Buffer shared) {
super("Consumer");
sharedLocation = shared;
}
public void run() {
//用于存放从缓冲区中取出的数
int sum = 0;
for(int count = 1; count <= 4; count++) {
try {
Thread.sleep((int)(Math.random()*3001));
sum = sum + sharedLocation.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(getName() + "read values totaling:" + sum +
"./nTerminating" + getName() + ".");
}
}
/**
*
* 测试入口
*
*/
public class MainTestShareBuffer {
public static void main(String[] args) {
/*创建缓冲区*/
UnsynchronizedBuffer sharedLocation = new UnsynchronizedBuffer();
/*创建生产者与消费者 出生状态*/
Producer producer = new Producer(sharedLocation);
Consumer consumer = new Consumer(sharedLocation);
/*进入就绪状态*/
producer.start();
consumer.start();
}
}
/**
*
* 生产者
*
*/
public class Producer extends Thread {
//缓冲区
private Buffer sharedLocation;
public Producer(Buffer shared) {
super("Producer");
sharedLocation = shared;
}
public void run() {
for(int count = 1; count <= 4; count++){
try {
//如果没有调用sleep方法,并且如果生产者首先执行,则生产者极有可能在消费者
//获得执行机会之前完成它的任务。如果消费者首先执行,则该线程极有可能 在生产 者
//获得执行机机会之前完成它的任务。
Thread.sleep((int)(Math.random()*3001));
//生产
sharedLocation.set(count);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//err打印错误流
System.err.println(getName() + "done producing." +
"/nTerminating" + getName() + "."); //Termainat终止
};
}
/**
*
* 具体的缓冲区
*
* 含生产者和消费者线程共享的整数,两线程由set和get方法访问整数
*
*/
public class UnsynchronizedBuffer implements Buffer{
/*共享的数据*/
private int buffer = -1;
/**
* 消费者消费一个
*/
public int get() {
// TODO Auto-generated method stub
System.err.println(Thread.currentThread().getName() + " reads " + buffer);
return buffer;
}
/**
* 生产者生产一个
*/
public void set(int value) {
// TODO Auto-generated method stub
buffer = value;
System.err.println(Thread.currentThread().getName() + " writes " + value);
}
}
本文出自 “baiyan425” 博客,请务必保留此出处http://baiyan425.blog.51cto.com/1573961/606709
*
* 共享缓冲区的接口
*
*/
public interface Buffer {
//生产者使用的
public abstract void set(int value);
//消费者使用的
public abstract int get();
}
/**
*
* 消费者
*
*/
public class Consumer extends Thread{
/*缓冲区*/
private Buffer sharedLocation;
public Consumer( Buffer shared) {
super("Consumer");
sharedLocation = shared;
}
public void run() {
//用于存放从缓冲区中取出的数
int sum = 0;
for(int count = 1; count <= 4; count++) {
try {
Thread.sleep((int)(Math.random()*3001));
sum = sum + sharedLocation.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(getName() + "read values totaling:" + sum +
"./nTerminating" + getName() + ".");
}
}
/**
*
* 测试入口
*
*/
public class MainTestShareBuffer {
public static void main(String[] args) {
/*创建缓冲区*/
UnsynchronizedBuffer sharedLocation = new UnsynchronizedBuffer();
/*创建生产者与消费者 出生状态*/
Producer producer = new Producer(sharedLocation);
Consumer consumer = new Consumer(sharedLocation);
/*进入就绪状态*/
producer.start();
consumer.start();
}
}
/**
*
* 生产者
*
*/
public class Producer extends Thread {
//缓冲区
private Buffer sharedLocation;
public Producer(Buffer shared) {
super("Producer");
sharedLocation = shared;
}
public void run() {
for(int count = 1; count <= 4; count++){
try {
//如果没有调用sleep方法,并且如果生产者首先执行,则生产者极有可能在消费者
//获得执行机会之前完成它的任务。如果消费者首先执行,则该线程极有可能 在生产 者
//获得执行机机会之前完成它的任务。
Thread.sleep((int)(Math.random()*3001));
//生产
sharedLocation.set(count);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//err打印错误流
System.err.println(getName() + "done producing." +
"/nTerminating" + getName() + "."); //Termainat终止
};
}
/**
*
* 具体的缓冲区
*
* 含生产者和消费者线程共享的整数,两线程由set和get方法访问整数
*
*/
public class UnsynchronizedBuffer implements Buffer{
/*共享的数据*/
private int buffer = -1;
/**
* 消费者消费一个
*/
public int get() {
// TODO Auto-generated method stub
System.err.println(Thread.currentThread().getName() + " reads " + buffer);
return buffer;
}
/**
* 生产者生产一个
*/
public void set(int value) {
// TODO Auto-generated method stub
buffer = value;
System.err.println(Thread.currentThread().getName() + " writes " + value);
}
}
本文出自 “baiyan425” 博客,请务必保留此出处http://baiyan425.blog.51cto.com/1573961/606709
相关文章推荐
- Thread--不同步的生产者与消费者
- boost c++ lib on linux(4) - thread同步条件变量学习——生产者消费者队列
- 资源同步与互斥问题(生产者与消费者)
- 生产者与消费者问题是典型的同步问题。这里简单介绍两种不同的实现方法。
- 进程同步的经典算法———生产者和消费者
- java多线程实现生产者/消费者同步
- 利用线程的同步和互斥解决两个消费者两个生产者一个临界区问题
- 进程同步---生产者与消费者问题以及进程同步机制--信号量及P、V操作
- 进程间同步互斥经典问题(一)生产者-消费者问题
- 同步线程--生产者与消费者模式
- Linux下进程的同步互斥实例——生产者消费者
- java多线程同步以及线程间通信详解&消费者生产者模式&死锁&Thread.join()(多线程编程之二)
- 学习和理解JAVA线程同步--生产者与消费者例子
- Linux互斥与同步应用(三):posix线程实现单个生产者和单个消费者模型
- 进程同步-生产者消费者问题
- 多线程间的同步-经典的生产者与消费者代码
- 线程学习二,notify和wait实现消费者-生产者同步问题
- Thread详解9:用wait/notify实现生产者/消费者模式
- java线程同步(生产者消费者应用-模拟叫号系统)
- 多线程--同步互斥&生产者消费者(一)