实现生产与消费【三种方法】

第一种方法

1、商品类

package com.wyq.thr;

public class Product {
	private String name;
	private String brand;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public Product(String name, String brand) {
		super();
		this.name = name;
		this.brand = brand;
	}
	public Product() {
		super();
	}
}

2、生产者

package com.wyq.thr;

public class Producer implements Runnable{
	Product pro = new Product();
	
	public Product getPro() {
		return pro;
	}

	public void setPro(Product pro) {
		this.pro = pro;
	}

	public Producer(Product pro) {
		super();
		this.pro = pro;
	}

	@Override
	public void run() {
		for(int i = 0;i<10;i++){
			if(i%2==0){
				pro.setBrand("旺仔");
				try {
					Thread.sleep(300);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				pro.setName("小馒头");
			}else{
				pro.setBrand("娃哈哈");
				try {
					Thread.sleep(300);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				pro.setName("矿泉水");
			}
			System.out.println(Thread.currentThread().getName()+"生产了"+pro.getBrand()+"牌子的"+pro.getName()+"——————————》"+i);
		}
	}
}

3、消费者

package com.wyq.thr;

public class Consumer implements Runnable{
	Product pro = new Product();

	public Product getPro() {
		return pro;
	}

	public void setPro(Product pro) {
		this.pro = pro;
	}

	public Consumer(Product pro) {
		super();
		this.pro = pro;
	}

	@Override
	public void run() {
		for(int i = 0;i<10;i++){
			System.out.println(Thread.currentThread().getName()+"正在消费"+pro.getBrand()+"的"+pro.getName()+"**"+i);
			
		}
		
	}

}

4、测试

package com.wyq.thr;

public class Test {
	public static void main(String[] args) {
		//创建对象
		Product pro = new Product();
		//创建对象
		Producer p = new Producer(pro);
		//创建代理类
		Thread t1 = new Thread(p, "生产者");
		Thread t2 = new Thread(p, "消费者");
		//启动
		t1.start();
		t2.start();
	}

}

第二种方法

1、类

package com.wyq.thr;

import java.util.concurrent.SynchronousQueue;

public class Product2 {
	private String name;
	private String brand;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public Product2(String name, String brand) {
		super();
		this.name = name;
		this.brand = brand;
	}
	public Product2() {
		super();
	}
	//生产者
	public synchronized void produ(String name, String brand){
		/**
		 * 在这里进行了参数的初始化
		 */
		this.setName(name);
		try {
			Thread.sleep(300);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.setBrand(brand);
		System.out.println(Thread.currentThread().getName()+"生产了"+this.getBrand()+"牌子的"+this.getName());
	}
	//消费者
	/**
	 * 消费者这里是不能传递参数的
	 */
	public synchronized void consu(){
		try {
			Thread.sleep(300);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getName()+"消费了————————》了"+this.getBrand()+"牌子的"+this.getName());
	}
}

2、生产者

package com.wyq.thr;

public class Producer2 implements Runnable{
	public Product2 pro;
	

	public Product2 getPro() {
		return pro;
	}


	public void setPro(Product2 pro) {
		this.pro = pro;
	}


	public Producer2(Product2 pro) {
		super();
		this.pro = pro;
	}


	public Producer2() {
		super();
	}


	@Override
	public void run() {
		
		for(int i=0;i<10;i++){
			/**
			 * 只有生产者这里进行传递了参数
			 */
			if(i%2==0){
				pro.produ("小馒头","旺仔");
			}else{
				pro.produ("矿泉水","娃哈哈");
			}
		}
		
	}
	

}

3、消费者

package com.wyq.thr;

public class Consumer2 implements Runnable{
	public Product2 pro;
	

	public Product2 getPro() {
		return pro;
	}


	public void setPro(Product2 pro) {
		this.pro = pro;
	}


	public Consumer2(Product2 pro) {
		super();
		this.pro = pro;
	}


	public Consumer2() {
		super();
	}


	@Override
	public void run() {
		for(int i = 0;i<10;i++){
			/**消费者这里是不能传递参数的
			 * 参数是由生产者这里进行传递的
			 */
			pro.consu();
		}
		
	}

}

4、测试

package com.wyq.thr;

public class Test2 {
	public static void main(String[] args) {
		
		Product2 pro = new Product2();
		Producer2 p = new Producer2(pro);
		Consumer2 c = new Consumer2(pro);
		Thread t1 = new Thread(p, "生产者");
		Thread t2 = new Thread(c, "消费者");
		t1.start();
		t2.start();
	}
}

第三种方法

扫描二维码关注公众号,回复: 6730126 查看本文章

1、类

package com.wyq.thr;

public class Product3 {
	private String name;
	private String brand;
	public boolean flag = false;//flag == true 代表有
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public Product3(String name, String brand) {
		super();
		this.name = name;
		this.brand = brand;
	}
	public Product3() {
		super();
	}
	public synchronized void produ(String name, String brand){
		
		if(flag){//falg = true 代表有    不生产 等待消费
			try {
				super.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.setBrand(brand);
		this.setName(name);
		System.out.println(Thread.currentThread().getName()+"生产了"+this.getBrand()+"牌子的"+this.getName());
		flag = true;
		super.notify();
		
	}
	public synchronized void consu(){
		if(!flag){//falg = false 代表没有  没有就等待生产  不消费
			try {
				super.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			Thread.sleep(300);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getName()+"消费了——————————————>"+this.getBrand()+"牌子的"+this.getName());
		flag = false;
		super.notify();
	}

}

2、生产者

package com.wyq.thr;

public class Producer3 implements Runnable{
	public Product3 pro;

	public Product3 getPro() {
		return pro;
	}

	public void setPro(Product3 pro) {
		this.pro = pro;
	}

	public Producer3(Product3 pro) {
		super();
		this.pro = pro;
	}

	public Producer3() {
		super();
	}

	@Override
	public void run() {
		for(int i = 0;i<10;i++){
			if(i%2==0){
				pro.produ("矿泉水", "娃哈哈");
			}else{
				pro.produ("小馒头", "旺仔");
			}
		}
	}
	

}

3、消费者

package com.wyq.thr;

public class Consumer3 implements Runnable{
	public Product3 pro;

	public Product3 getPro() {
		return pro;
	}

	public void setPro(Product3 pro) {
		this.pro = pro;
	}

	public Consumer3(Product3 pro) {
		super();
		this.pro = pro;
	}

	public Consumer3() {
		super();
	}

	@Override
	public void run() {
		for(int i = 0;i<10;i++){
			pro.consu();
		}
		
	}

}

4、测试类

package com.wyq.thr;

public class Test3 {
	public static void main(String[] args) {
		
		Product3 pro = new Product3();
		Producer3 p = new Producer3(pro);
		Consumer3 c = new Consumer3(pro);
		Thread t1 = new Thread(p, "生产者");
		Thread t2 = new Thread(c, "消费者");
		t1.start();
		t2.start();
	}
}

总结:

1)使用this.set()方法给参数进行初始化

2)使用this.get()获取参数的值

3)写成方法进行调用

4)synchronized同步方法的使用时:public synchronized void get(){}

猜你喜欢

转载自blog.csdn.net/wyqwilliam/article/details/94484283
今日推荐