java再复习——线程的经典问题-生产者消费者
2017-03-08 10:13
453 查看
为什么多线程总是离不开这个问题呢?
因为这种场景是多线程在生成环境中最重要,最主要的应用,基于这种场景提炼出的问题就是生产者消费者问题。
实例代码:
/**
* 资源
* @author PC
*
*/
class Resource {
int count = 0;
boolean isHas = false;
public synchronized void product() throws InterruptedException{
while(isHas){
wait(); //为了方便阅读 ,异常直接抛出了。
}
System.out.println(Thread.currentThread().getName() + "..生成了商品【"+(++count)+"】----");
isHas = true;
notifyAll();
}
public synchronized void consume() throws InterruptedException{
while(!isHas){
wait();
}
System.out.println(Thread.currentThread().getName() + "..消费了商品【"+count+"】");
isHas = false;
notifyAll();
}
}
/**
* 生产者
* @author PC
*
*/
class Producer implements Runnable{
Resource resource;
public Producer(Resource resource){
this.resource = resource;
}
public void run() {
while (true) {
try {
resource.product();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* 消费者
* @author PC
*
*/
class Consumer implements Runnable{
Resource resource;
public Consumer(Resource resource){
this.resource = resource;
}
public void run() {
while (true) {
try {
resource.consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
测试代码:
public class ProducerConsumerDemo {
public static void main(String[] args) {
Resource resource = new Resource();
new Thread(new Producer(resource)).start();
new Thread(new Producer(resource)).start();
new Thread(new Consumer(resource)).start();
new Thread(new Consumer(resource)).start();
}
}
因为这种场景是多线程在生成环境中最重要,最主要的应用,基于这种场景提炼出的问题就是生产者消费者问题。
实例代码:
/**
* 资源
* @author PC
*
*/
class Resource {
int count = 0;
boolean isHas = false;
public synchronized void product() throws InterruptedException{
while(isHas){
wait(); //为了方便阅读 ,异常直接抛出了。
}
System.out.println(Thread.currentThread().getName() + "..生成了商品【"+(++count)+"】----");
isHas = true;
notifyAll();
}
public synchronized void consume() throws InterruptedException{
while(!isHas){
wait();
}
System.out.println(Thread.currentThread().getName() + "..消费了商品【"+count+"】");
isHas = false;
notifyAll();
}
}
/**
* 生产者
* @author PC
*
*/
class Producer implements Runnable{
Resource resource;
public Producer(Resource resource){
this.resource = resource;
}
public void run() {
while (true) {
try {
resource.product();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* 消费者
* @author PC
*
*/
class Consumer implements Runnable{
Resource resource;
public Consumer(Resource resource){
this.resource = resource;
}
public void run() {
while (true) {
try {
resource.consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
测试代码:
public class ProducerConsumerDemo {
public static void main(String[] args) {
Resource resource = new Resource();
new Thread(new Producer(resource)).start();
new Thread(new Producer(resource)).start();
new Thread(new Consumer(resource)).start();
new Thread(new Consumer(resource)).start();
}
}
相关文章推荐
- 再谈java线程以及经典的生产者,消费者问题
- Java- 简单了解线程 生产者与消费者问题(三)
- Java经典线程同步问题------生产者与消费者
- Java- 简单了解线程 生产者与消费者问题(三)
- 生产者消费者问题之Java线程实现
- JAVA synchronized 线程经典 生产者消费者 两个完全不同的实现的方式
- Java线程(三):线程协作-生产者/消费者问题
- java多线程之生产者消费者经典问题
- 线程经典,生产者与消费者问题
- Java基础_线程_多线程_生产者消费者问题
- java基础---->线程 生产者消费者问题
- java生产者消费者问题(线程同步与线程等待的应用)
- 经典线程同步问题(生产者&消费者)--Java实现
- java 多线程经典例子——生产者与消费者的问题
- java基础---->线程 生产者消费者问题
- java多线程总结六:经典生产者消费者问题实现
- Java 线程的基本概念(模仿生产者与消费者的问题)
- java生产者消费者问题(线程同步与线程等待的应用)
- 用C#线程技术模拟“生产者-消费者”经典进程同步问题
- java线程:三种方式实现生产者消费者问题_2