六种方式实现生产者消费者(未完)
六种方式实现生产者消费者(未完)
特拉仔 发表于6个月前
六种方式实现生产者消费者(未完)
  • 发表于 6个月前
  • 阅读 1
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

一、利用Object对象是wait和notify\notifyAll

    

package com.jv.parallel.consumerandproducer.objectwait;

public class Car {
	private volatile int flag = 0;
	public void showConsumer(){
		System.out.println("I am a Car ! 我已经被消费");
	}
	public void showProducer(){
		System.out.println("I am a Car ! 我已经被生产");
	}
	public int getFlag(){
		return flag;
	}
	public void setFlag(int flag){
		this.flag=flag;
	}
}

 

package com.jv.parallel.consumerandproducer.objectwait;

public class Consumer extends Thread{
	private Car car;
	
	public Consumer() {
		// TODO Auto-generated constructor stub
	}
	
	public Consumer(Car car){
		this.car = car;
	}
	
	@Override
	public void run() {
		while(true){
			synchronized(car){
				try {
					while(car.getFlag()==0){
							car.wait();
					}
						car.setFlag(0);
						car.showConsumer();
						car.notifyAll();
						Thread.currentThread().sleep(1000);
					
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

 

package com.jv.parallel.consumerandproducer.objectwait;

public class Producer extends Thread{
	private Car car;
	
	public Producer(){
		
	}
	
	public Producer(Car car){
		this.car = car;
	}
	
	public void run() {
		while(true){
			synchronized(car){
				try {
					while(car.getFlag()==1){
							car.wait();
					}
						car.setFlag(1);
						car.showProducer();
						car.notifyAll();
						Thread.currentThread().sleep(1000);
					
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

 

package com.jv.parallel.consumerandproducer.objectwait;

public class Test {
	public static void main(String[] args) {
		Car car  = new Car();
		Consumer cs = new Consumer(car);
		Producer pd = new Producer(car);
		cs.start();
		pd.start();
	}
}

 

二、使用Lock.Condition

    

package com.jv.parallel.consumerandproducer.lock;

public class Car {
	private volatile int flag = 0;
	public void showConsumer(){
		System.out.println("I am a Car ! 我已经被消费");
	}
	public void showProducer(){
		System.out.println("I am a Car ! 我已经被生产");
	}
	public int getFlag(){
		return flag;
	}
	public void setFlag(int flag){
		this.flag=flag;
	}
}
package com.jv.parallel.consumerandproducer.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

public class Consumer implements Runnable{
	
	private Car car;
	private Lock lock;
	private Condition cd ;
	
	public Consumer() {
		// TODO Auto-generated constructor stub
	}
	
	public Consumer(Car car,Lock lock,Condition cd){
		this.car=car;
		this.lock=lock;
		this.cd=cd;
	}

	@Override
	public void run() {
		while(true){
			lock.lock();
			try{
				while(car.getFlag()==0){
					cd.await();
				}
					car.setFlag(0);
					car.showConsumer();
					cd.signal();
					Thread.currentThread().sleep(1000);
				
			} catch (InterruptedException e) {
				e.printStackTrace();
			}finally{
				lock.unlock();
			}
		}
	}
}
package com.jv.parallel.consumerandproducer.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

public class Producer implements Runnable{
	private Car car;
	private Lock lock;
	private Condition cd ;
	
	public Producer() {
		// TODO Auto-generated constructor stub
	}
	
	public Producer(Car car,Lock lock,Condition cd){
		this.car=car;
		this.lock=lock;
		this.cd=cd;
	}

	@Override
	public void run() {
		while(true){
			lock.lock();
			try{
				while(car.getFlag()==1){
					cd.await();
				}
					car.setFlag(1);
					car.showProducer();
					cd.signal();
					Thread.currentThread().sleep(1000);
				
			} catch (InterruptedException e) {
				e.printStackTrace();
			}finally{
				lock.unlock();
			}
		}
	}
}
package com.jv.parallel.consumerandproducer.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test {
	public static void main(String[] args) {
		Lock lock = new ReentrantLock();
		Car car = new Car();
		Condition cd = lock.newCondition();
		Thread t1 = new Thread(new Consumer(car,lock,cd));
		Thread t2 = new Thread(new Producer(car,lock,cd));
		t1.start();
		t2.start();
	}
}

 

三、使用阻塞队列

设置阻塞队列长度为1,使用put和take方法(可阻塞)

package com.jv.consumerandproducer.queue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class TestQueue {	
	public static void main(String[] args) {
		BlockingQueue<Car> queue = new ArrayBlockingQueue<Car>(1);
		Consumer c = new Consumer(queue);
		Producer p = new Producer(queue);
		Thread t1 = new Thread(c);
		Thread t2 = new Thread(p);
		t1.start();
		t2.start();
		System.out.println("main haved end");
	}
	
}
class Consumer implements Runnable{
	BlockingQueue<Car> queue;
	public Consumer(BlockingQueue<Car> queue){
		this.queue=queue;
	}

	@Override
	public void run() {
		Car c ;
		while(true){
			try {
				c = queue.take();
				c.showConsumer();
				Thread.currentThread().sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
}

class Producer implements Runnable{
	BlockingQueue<Car> queue;
	public Producer(BlockingQueue<Car> queue){
		this.queue=queue;
	}
	@Override
	public void run() {
		Car c ;
		while(true){
			try{
				c = new Car();
				queue.put(c);
				c.showProducer();
				Thread.currentThread().sleep(1000);
			}catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
}
package com.jv.consumerandproducer.queue;

public class Car {
	private volatile int flag = 0;
	public void showConsumer(){
		System.out.println("I am a Car ! 我已经被消费");
	}
	public void showProducer(){
		System.out.println("I am a Car ! 我已经被生产");
	}
	public int getFlag(){
		return flag;
	}
	public void setFlag(int flag){
		this.flag=flag;
	}
}

四、利用FutureTask实现

    还需要研究它的内部实现原理

    代码待补充

五、使用信号量实现

    其内部原理还是利用被包装过的同步器的condition对象实现

    代码待补充

六、使用Redis列表数据类型实现

    因为它具有可阻塞特性。

    代码待补充

共有 人打赏支持
粉丝 2
博文 73
码字总数 39148
×
特拉仔
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: