生产者和消费者模式(管程法SynContainer):
我们知道wait()\notify()\notifyAll() 方法都是java.lang.Object类的 native方法,如果我们要在某个线程中的synchronized块中调用 wait()\notify()\notifyAll() 这几个方法,有且只能调用线程所持有的那个锁的对象的wait()\notify()\notifyAll()方法。如果当前线程不是对象锁的持有者,则持有锁的方法就会抛出一个java.lang.IllegalMonitorStateException异常。
java 中任何类都是从java.lang.Object类的子类,那么任何类就继承了java.lang.Object类的公有的wait()\notify()\notifyAll() 这几个native方法。
若线程持有的是当前对象的锁那么直接调用wait()、super.wait()、this.wait(),都是直接调用的当前对象的方法。所以在此处代码中调用wait()\notify()、super.wait()\super.notify、this.wait()\this.notify是一样的。
package com.kuang;
//测试:生产者消费者模式===》利用缓冲区解决:官程法
public class Test {
public static void main(String[] args) {
SynContainer synContainer=new SynContainer();
Producter producter = new Producter(synContainer);
Consumer consumer = new Consumer(synContainer);
producter.start();
consumer.start();
}
}
class Chicken{
int id;
public Chicken(int id){
this.id=id;
}
}
class Producter extends Thread{
SynContainer synContainer;
public Producter(SynContainer synContainer){
this.synContainer=synContainer;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {//这个i用来表示鸡的编号,从1到100
synContainer.push(new Chicken(i));
System.out.println("生产了"+i+"只鸡");
}
}
}
class Consumer extends Thread{
SynContainer synContainer;
public Consumer(SynContainer synContainer){
this.synContainer=synContainer;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
Chicken chicken=synContainer.pop();
System.out.println("消费了"+chicken.id+"只鸡");
}
}
}
class SynContainer{
Chicken[] chickens=new Chicken[10];//
int count=0;//计数器,用来表示容器中现有的鸡的个数
public synchronized void push(Chicken chicken){
if(count==chickens.length){
//说明不能生产了应该消费了
try {
this.wait();//或者直接写成wait()也可以,表示当前持有锁的对象需要进入等待,这个方法是给生产者使用的
} catch (InterruptedException e) {
e.printStackTrace();
}
}
chickens[count]=chicken;//从0开始,0,1,2,3
count++;
//可以通知消费者消费了,由于是
this.notifyAll();
}
public synchronized Chicken pop(){
if(count==0){
//说明不能消费了只能生产了
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count--;
Chicken chicken=chickens[count];
this.notifyAll();
return chicken;
}
}
使用阻塞队列(BlockingQueue):
package com.nowcoder.community;
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class BlockingQueueTests {
public static void main(String[] args) {
BlockingQueue queue=new ArrayBlockingQueue(10);
//10表示队列的容量的大小
//把这个队列作为参数传输到生产者和消费者的线程中
new Thread(new Producer(queue)).start();
new Thread(new Consumer(queue)).start();
new Thread(new Consumer(queue)).start();
new Thread(new Consumer(queue)).start();
//一个生产者,三个消费者
}
}
class Producer implements Runnable{
private BlockingQueue<Integer> queue;
public Producer(BlockingQueue<Integer> queue){
this.queue=queue;
}
@Override
public void run(){
try{
for (int i=0;i<100;i++){
Thread.sleep(20);
queue.put(i);
System.out.println(Thread.currentThread().getName()+"生产:"+queue.size());
}
}catch (Exception e){
e.printStackTrace();
}
}
}
class Consumer implements Runnable{
private BlockingQueue<Integer> queue;
public Consumer(BlockingQueue<Integer> queue){
this.queue=queue;
}
@Override
public void run(){
try{
while(true){
Thread.sleep(new Random().nextInt(1000));
queue.take();
System.out.println(Thread.currentThread().getName()+"消费了:"+queue.size());
}
}catch (Exception e){
e.printStackTrace();
}
}
}