同步线程--生产者与消费者模式
2006-10-18 14:51
169 查看
同步线程的一种经典设计模式。
public class Test {
public static void main(String[] args) {
Quene q = new Quene();
Producter producter = new Producter(q);
Customer customer = new Customer(q);
producter.start();
customer.start();
}
}
//生产者
public class Producter extends Thread {
private Quene quene;
public Producter(Quene quene) {
this.quene = quene;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
quene.setItem(i);
System.out.println("生产者:生产产品" + i);
}
}
}
//消费者
public class Customer extends Thread {
private Quene quene;
public Customer(Quene quene) {
this.quene = quene;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("消费者:获得产品" + quene.getItem());
}
}
}
//销售列队
public class Quene {
private int item;
private boolean isFull = false;
public synchronized int getItem() {
if (!isFull) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
isFull = false;
this.notify();
return item;
}
//因为使用notify()所有要加上 synchronized
//synchronized 对方法修饰的时候等同与
// synchronized(this){ ...... }
public synchronized void setItem(int item) {
if (!isFull) {
this.item = item;
isFull = true;
this.notify();
}
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Test {
public static void main(String[] args) {
Quene q = new Quene();
Producter producter = new Producter(q);
Customer customer = new Customer(q);
producter.start();
customer.start();
}
}
//生产者
public class Producter extends Thread {
private Quene quene;
public Producter(Quene quene) {
this.quene = quene;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
quene.setItem(i);
System.out.println("生产者:生产产品" + i);
}
}
}
//消费者
public class Customer extends Thread {
private Quene quene;
public Customer(Quene quene) {
this.quene = quene;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("消费者:获得产品" + quene.getItem());
}
}
}
//销售列队
public class Quene {
private int item;
private boolean isFull = false;
public synchronized int getItem() {
if (!isFull) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
isFull = false;
this.notify();
return item;
}
//因为使用notify()所有要加上 synchronized
//synchronized 对方法修饰的时候等同与
// synchronized(this){ ...... }
public synchronized void setItem(int item) {
if (!isFull) {
this.item = item;
isFull = true;
this.notify();
}
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
相关文章推荐
- 生产者与消费者模式(线程的同步与互斥)
- Java线程同步与死锁、生产者消费者模式以及任务调度等
- 进程与线程,同步与死锁,生产者消费者模式
- JAVA线程同步——消费者和生产者模式
- 多线程---使用ManualResetEvent来控制线程间的同步(实现了消费者和生产者模式)
- 生产者与消费者模式(线程的同步与互斥)
- 多线程---使用ManualResetEvent来控制线程间的同步(实现了消费者和生产者模式)
- Java线程间同步实现生产者-消费者模式
- Qt线程同步之一个生产者多个消费者
- 线程的同步和通讯——生产者和消费者 (VS .NET控制台)
- Java线程(2)线程协作-生产者/消费者模式
- 线程协作-生产者/消费者模式
- 进程、线程知识点总结和同步(消费者生产者,读者写者三类问题)、互斥、异步、并发、并行、死锁、活锁的总结
- java多线程同步以及线程间通信详解&消费者生产者模式&死锁&Thread.join()(多线程编程之二)
- Java Note: 多线程的同步(互斥锁)的方法对比,信号量锁,读写锁的实现,生产者-消费者模式的实现
- Java线程(三):线程协作-生产者/消费者模式 .
- Java线程(三):线程协作-生产者/消费者模式
- java多线程同步以及线程间通信详解&消费者生产者模式&死锁&Thread.join()(多线程编程之二)
- Java线程同步:生产者-消费者 模型(代码示例)
- Java线程同步:生产者-消费者 模型(代码示例)