Java基础多线程通讯之生产者消费者模式示例:
2013-01-15 12:07
295 查看
class ProducerConsumerDemo
{
public static void main(String[] args)
{
Product product = new Product();
new Thread(new Producer(product)).start();
new Thread(new Consumer(product)).start();
}
}
class Product
{
private int count;
private boolean flag;
public synchronized void create()
{
while(this.flag)
{
try
{
this.wait();
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
this.count++;
System.out.println(Thread.currentThread().getName()+" create -- :"+this.count);
this.flag = true;
this.notify();
try
{
Thread.sleep(300);
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
public synchronized void get()
{
while(!this.flag)
{
try
{
this.wait();
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
System.out.println(Thread.currentThread().getName()+" sale:"+this.count);
this.flag = false;
this.notify();
try
{
Thread.sleep(600);
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
}
class Producer implements Runnable
{
private Product product;
public Producer(Product product)
{
this.product = product;
}
public void run()
{
while(true)
{
product.create();
}
}
}
class Consumer implements Runnable
{
private Product product;
public Consumer(Product product)
{
this.product = product;
}
public void run()
{
while(true)
{
product.get();
}
}
}
{
public static void main(String[] args)
{
Product product = new Product();
new Thread(new Producer(product)).start();
new Thread(new Consumer(product)).start();
}
}
class Product
{
private int count;
private boolean flag;
public synchronized void create()
{
while(this.flag)
{
try
{
this.wait();
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
this.count++;
System.out.println(Thread.currentThread().getName()+" create -- :"+this.count);
this.flag = true;
this.notify();
try
{
Thread.sleep(300);
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
public synchronized void get()
{
while(!this.flag)
{
try
{
this.wait();
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
System.out.println(Thread.currentThread().getName()+" sale:"+this.count);
this.flag = false;
this.notify();
try
{
Thread.sleep(600);
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
}
class Producer implements Runnable
{
private Product product;
public Producer(Product product)
{
this.product = product;
}
public void run()
{
while(true)
{
product.create();
}
}
}
class Consumer implements Runnable
{
private Product product;
public Consumer(Product product)
{
this.product = product;
}
public void run()
{
while(true)
{
product.get();
}
}
}
相关文章推荐
- Java基础多线程间通讯之多生产者、多消费者模式示例:
- Java基础多线程间通讯同步操作示例一(未优化):
- Java基础多线程间通讯示例操作(已优化)二:
- 黑马程序员——JAVA基础之多线程的线程间通讯等
- Java基础之多线程(二)--通讯、生产者消费者、停止、Jion方法
- 黑马程序员-Java基础-多线程间通讯
- java基础多线程之销售示例:
- Java基础多线程之线程中止示例:
- Java基础系列:(4)多线程的一些用法示例
- java多线程基础(二)理解中断
- JAVA基础知识之多线程——线程组和未处理异常
- 【Java基础】多线程
- Java基础----多线程程序解决
- Java多线程基础--03之 Thread中start()和run()的区别
- 黑马程序员-【java基础】-多线程
- Java多线程基础之常用方法
- Java基础练习题 (8)多线程
- day1 java基础回顾-多线程
- JAVA多线程(五)用lock、synchronized、阻塞队列三种方法实现生产者消费者模式
- IT十八掌作业_java基础第九天_多线程、自动拆装箱