------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
由于现在对生产者消费者类似的功能,缺乏实际的运用,所以不灵活,希望通过提出需求的方式,记住下面代码中能够实现的功能,作为经验积累起来。
需求:
1. 生产者可以有多个,消费者也可以有多个。
Consumer c1 = new Consumer(s);
Consumer c2 = new Consumer(s);
Consumer c3 = new Consumer(s);
Producer p1 = new Producer(s);
Producer p2 = new Producer(s);
2. 生产线和消费线都在不停的工作。
public void run() {
while(true) {
s.comsume();
}
}
3. 但生产线同一时间只能有一个生产者工作(对生产方法添加监听器),生产出产品后就停止(使用wait()方法停止,直到被唤醒),等待或唤醒消费者消费。
public synchronized void produce(String name) {
this.name = name;
while(flag) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
count++;
flag = true;
System.out.println(Thread.currentThread().getName() + "生产了第:" + count + "个" + this.name);
notifyAll();
}
4. 消费者可以消费时,同一时间只能有一个消费者消费,消费完毕后就停止消费,等待生产者生产。
思路:
1. 所有的生产者和消费者同时在对同一个产品做处理,所以把该产品封装成单独的类,让生产者消费者处理一个对象。
2. 对象的成员中,提供计数器count;真则生产,假则消费的标记符flag;
private String name ;
private int count = 0;
private boolean flag = false;
3. 生产者操作该对象时使用它的生产方法produce(),消费者操作该对象时使用对象的消费方法consume().
public synchronized void produce(String name) {
// 代码体
}
public synchronized void comsume() {
// 代码体
}
4. 无限循环写在run方法中。因为启动一个线程通过start()方法,虚拟机会调用实现了Runnable接口的对象的run()方法,或是继承了Thread类的对象调用其重写了的run()方法。
public void run() {
while(true) {
s.comsume();
}
}
源码:
package com.lxh.iterview;
public class CustomerProducer {
public static void main(String[] args) {
Source s = new Source();
Consumer c1 = new Consumer(s);
Consumer c2 = new Consumer(s);
Consumer c3 = new Consumer(s);
Producer p1 = new Producer(s);
Producer p2 = new Producer(s);
Thread t1 = new Thread(c1);
Thread t2 = new Thread(c2);
Thread t3 = new Thread(c3);
Thread t4 = new Thread(p1);
Thread t5 = new Thread(p2);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
class Source {
private String name ;
private int count = 0;
private boolean flag = false;
public synchronized void produce(String name) {
this.name = name;
while(flag) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
count++;
flag = true;
System.out.println(Thread.currentThread().getName() + "生产了第:" + count + "个" + this.name);
notifyAll();
}
public synchronized void comsume() {
while(!flag) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
flag = false;
System.out.println("---" + Thread.currentThread().getName() + "消费了第:" + count + "个" + this.name);
notifyAll();
}
}
class Consumer implements Runnable {
Source s = null;
public Consumer(Source s) {
this.s = s;
}
public void run() {
while(true) {
s.produce("汉堡");
}
}
}
class Producer implements Runnable {
Source s = null;
public Producer(Source s) {
this.s = s;
}
public void run() {
while(true) {
s.comsume();
}
}
}
package com.lxh.iterview;
public class CustomerProducer {
public static void main(String[] args) {
Source s = new Source();
Consumer c1 = new Consumer(s);
Consumer c2 = new Consumer(s);
Consumer c3 = new Consumer(s);
Producer p1 = new Producer(s);
Producer p2 = new Producer(s);
Thread t1 = new Thread(c1);
Thread t2 = new Thread(c2);
Thread t3 = new Thread(c3);
Thread t4 = new Thread(p1);
Thread t5 = new Thread(p2);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
class Source {
private String name ;
private int count = 0;
private boolean flag = false;
public synchronized void produce(String name) {
this.name = name;
while(flag) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
count++;
flag = true;
System.out.println(Thread.currentThread().getName() + "生产了第:" + count + "个" + this.name);
notifyAll();
}
public synchronized void comsume() {
while(!flag) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
flag = false;
System.out.println("---" + Thread.currentThread().getName() + "消费了第:" + count + "个" + this.name);
notifyAll();
}
}
class Consumer implements Runnable {
Source s = null;
public Consumer(Source s) {
this.s = s;
}
public void run() {
while(true) {
s.produce("汉堡");
}
}
}
class Producer implements Runnable {
Source s = null;
public Producer(Source s) {
this.s = s;
}
public void run() {
while(true) {
s.comsume();
}
}
}