Thread--不同步的生产者与消费者
2013-01-17 11:16
127 查看
/**
*
* 共享缓冲区的接口
*
*/
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/1120452
*
* 共享缓冲区的接口
*
*/
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/1120452
相关文章推荐
- boost c++ lib on linux(4) - thread同步条件变量学习——生产者消费者队列
- Thread--不同步的生产者与消费者
- 基于并发包同步机制实现生产者-消费者模式
- JAVA线程同步——消费者和生产者模式
- 多生产者--多消费者 显示锁实现同步 java实现
- 进程同步---生产者与消费者问题以及进程同步机制--信号量及P、V操作
- 并发与同步、信号量与管程、生产者消费者问题
- 学习和理解JAVA线程同步--生产者与消费者例子
- 在程序中利用信号量同步来模拟生产者与消费者
- 多生产者多消费者基于POSIX版本信号量的同步与互斥
- Linux相互排斥与同步应用(三):posix线程实现单个生产者和单个消费者模型
- [笔记][Java7并发编程实战手册]2.4在同步代码中使用条件-生产者与消费者
- 线程同步之生产者消费者
- Thread中的生产者消费者
- java基础知识回顾之java Thread类学习(八)--java多线程通信等待唤醒机制经典应用(生产者消费者)
- 同步同容器类和工具类2——阻塞队列和生产者-消费者模式
- Java实现生产者与消费者的同步
- 进程同步--生产者消费者问题(Producer-consumer Problem)
- 进程同步---生产者与消费者问题以及进程同步机制--信号量及P、V操作
- 同步规范------生产者与消费者(二)