单生产单消费
商品类
class Goods{
private String goodsName;//商品名称
private int count;//商品库存
//生产方法
public synchronized void set(String goodsName){
this.goodsName = goodsName;
this.count++;
System.out.println(Thread.currentThread().getName()+"生产"+toString());
}
//消费方法
public synchronized void get(){
//每次消费一次商品
this.count--;
System.out.println(Thread.currentThread().getName()+"消费"+toString());
}
@Override
public String toString() {
return "Goods{" +
"goodsName='" + goodsName + '\'' +
", count=" + count +
'}';
}
}
生产者类
class Producer implements Runnable{
private Goods goods;
public Producer(Goods goods) {
this.goods = goods;
}
@Override
public void run() {
this.goods.set("生产奔驰C200L");
}
}
消费者类
class Consumer implements Runnable{
private Goods goods;
public Consumer(Goods goods) {
this.goods = goods;
}
@Override
public void run() {
this.goods.get();
}
}
测试类
public class CPTest {
public static void main(String[] args) throws InterruptedException {
Goods goods = new Goods();
Thread producerThread = new Thread(new Producer(goods),"生产者线程");
Thread consumerThread = new Thread(new Consumer(goods),"消费者线程");
producerThread.start();
Thread.sleep(1000);
consumerThread.start();
}
}
注意:如果先开启消费者线程后开启生产者线程就会导致count为负数。此时就要用到我们的wait()和notify()。
class Goods{
private String goodsName;//商品名称
private int count;//商品库存
//生产方法
public synchronized void set(String goodsName) throws InterruptedException {
if (count>0){
System.out.println("此时还有商品,一会儿再生产......");
this.wait();
}
this.goodsName = goodsName;
this.count++;
System.out.println(Thread.currentThread().getName()+"生产"+toString());
notify();
}
//消费方法
public synchronized void get() throws InterruptedException {
if (count==0){
System.out.println("商品已经售罄,等待货品上架.....");
this.wait();
}
//每次消费一次商品
this.count--;
System.out.println(Thread.currentThread().getName()+"消费"+toString());
notify();
}
@Override
public String toString() {
return "Goods{" +
"goodsName='" + goodsName + '\'' +
", count=" + count +
'}';
}
}
单商品多生产多消费
class Goods{
private String goodsName;//商品名称
private int count;//商品库存
//生产方法
public synchronized void set(String goodsName) throws InterruptedException {
while (count>0){
System.out.println("此时还有商品,一会儿再生产......");
this.wait();
}
this.goodsName = goodsName;
this.count++;
System.out.println(Thread.currentThread().getName()+"生产"+toString());
notifyAll();
}
//消费方法
public synchronized void get() throws InterruptedException {
while (count==0){
System.out.println("商品已经售罄,等待货品上架.....");
this.wait();
}
//每次消费一次商品
this.count--;
System.out.println(Thread.currentThread().getName()+"消费"+toString());
notifyAll();
}
@Override
public String toString() {
return "Goods{" +
"goodsName='" + goodsName + '\'' +
", count=" + count +
'}';
}
}
//------------------------------------------------------------------
//生产者类
class Producer implements Runnable{
private Goods goods;
public Producer(Goods goods) {
this.goods = goods;
}
@Override
public void run() {
while (true){
try {
this.goods.set("生产奔驰C200L");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//------------------------------------------------------------------
//消费者类
class Consumer implements Runnable{
private Goods goods;
public Consumer(Goods goods) {
this.goods = goods;
}
@Override
public void run() {
while (true){
try {
this.goods.get();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//-------------------------------------------------------------
//测试类
public class CPTest {
public static void main(String[] args) throws InterruptedException {
Goods goods = new Goods();
Thread producerThread1 = new Thread(new Producer(goods),"生产者线程1");
Thread producerThread2 = new Thread(new Producer(goods),"生产者线程2");
Thread producerThread3 = new Thread(new Producer(goods),"生产者线程3");
Thread producerThread4 = new Thread(new Producer(goods),"生产者线程4");
Thread consumerThread1 = new Thread(new Consumer(goods),"消费者线程1");
Thread consumerThread2 = new Thread(new Consumer(goods),"消费者线程2");
Thread consumerThread3 = new Thread(new Consumer(goods),"消费者线程3");
consumerThread1.start();
consumerThread2.start();
consumerThread3.start();
producerThread1.start();
producerThread2.start();
producerThread3.start();
producerThread4.start();
}
}
多商品多生产多消费
class Goods{
private String goodsName;//商品名称
private int count;//商品库存
private int maxCount;
public Goods(int maxCount) {
this.maxCount = maxCount;
}
//生产方法
public synchronized void set(String goodsName) throws InterruptedException {
while (count==maxCount){
System.out.println("此时还有商品,一会儿再生产......");
this.wait();
}
this.goodsName = goodsName;
this.count++;
System.out.println(Thread.currentThread().getName()+"生产"+toString());
notifyAll();
}
//消费方法
public synchronized void get() throws InterruptedException {
while (count==0){
System.out.println("商品已经售罄,等待货品上架.....");
this.wait();
}
//每次消费一次商品
this.count--;
System.out.println(Thread.currentThread().getName()+"消费"+toString());
notifyAll();
}
@Override
public String toString() {
return "Goods{" +
"goodsName='" + goodsName + '\'' +
", count=" + count +
'}';
}
}
//------------------------------------------------------------------
//生产者类
class Producer implements Runnable{
private Goods goods;
public Producer(Goods goods) {
this.goods = goods;
}
@Override
public void run() {
while (true){
try {
this.goods.set("生产奔驰C200L");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//------------------------------------------------------------------
//消费者类
class Consumer implements Runnable{
private Goods goods;
public Consumer(Goods goods) {
this.goods = goods;
}
@Override
public void run() {
while (true){
try {
this.goods.get();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//-------------------------------------------------------------
//测试类
public class CPTest {
public static void main(String[] args) throws InterruptedException {
Goods goods = new Goods(10);
Thread producerThread1 = new Thread(new Producer(goods),"生产者线程1--");
Thread producerThread2 = new Thread(new Producer(goods),"生产者线程2--");
Thread producerThread3 = new Thread(new Producer(goods),"生产者线程3--");
Thread producerThread4 = new Thread(new Producer(goods),"生产者线程4--");
Thread consumerThread1 = new Thread(new Consumer(goods),"消费者线程1--");
Thread consumerThread2 = new Thread(new Consumer(goods),"消费者线程2--");
Thread consumerThread3 = new Thread(new Consumer(goods),"消费者线程3--");
consumerThread1.start();
consumerThread2.start();
consumerThread3.start();
producerThread1.start();
producerThread2.start();
producerThread3.start();
producerThread4.start();
}
}