用synchonized实现多线程的消费者和生产者的模拟

本文通过实现一个简单的消费者与生产者模型,详细解释了如何使用synchronized关键字来确保多线程环境下资源的正确访问。通过模拟消费者和生产者对FoodBasket资源的操作,展示了等待和通知机制在多线程同步中的应用。

主要涉及的内容是对synchonized关键字的理解,以及如果同步线程对同一资源的访问请求。下面这个程序主要就是模仿消费者和生产者的关系。
主要由一下四部分组成:
1 物品定义 2 消费者定义 3 生产者定义 4 测试体定义

FoodBasket的定义:

package languagelearning.multithread;

public class FoodBasket {

	public static int counter=0;
	
	//consumer
	 public void eatOne(String name) throws InterruptedException{
		synchronized(this) {
			if (counter == 0){
					System.out.println(name + " say: No food and I will wait ");
					wait();
			} else {
				System.out.println(name +" say: eat one and now counter is " +(--counter));
				Thread.sleep(200);
				notify();
			}
		}
		Thread.sleep(2000);
	}

	//producer
	public void prouduceOne(String name) throws InterruptedException {
		synchronized (this){
			if (counter >= 20){
				System.out.println(name + " say :there cannot hold more food and I sleep for a while");
				wait();
			}else {
				System.out.println(name  + " say:  I produce one and now counter is " + (++counter));
				Thread.sleep(400);
				notify();
			}
		}
		Thread.sleep(2000);
	}
	
	public  static void main(String[] args) {
		
	}
}

Consumer的定义:

package languagelearning.multithread;

public class Consumer extends Thread {
	private FoodBasket t;
	private String name ;
	
	public Consumer(FoodBasket t,String name) {
		this.t = t;
		this.name = name;
	}
	public void run () {
		while(true) {
			try {
				t.eatOne(name);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

生产者定义:

package languagelearning.multithread;

public class Producer extends Thread {
	private FoodBasket t;
	private String name;
	public Producer(FoodBasket t, String name) {
		this.t = t;
		this.name = name;
	}
	public Producer(FoodBasket t) {
		this.t = t;
	}
	public void run() {
		while(true) {
			try {
				t.prouduceOne(name);
			} catch (InterruptedException e) {
					e.printStackTrace();
			}
		}
	}

}

测试主体定义:

package languagelearning.multithread;

public class ConsumerAndProducer {

	public static void main(String[] args) throws InterruptedException {
		FoodBasket t = new FoodBasket();
		Consumer consumer1 = new Consumer(t, "consumer1");
		Consumer consumer2=  new Consumer(t, "consumer2");
		consumer1.start();
		consumer2.start();
		
		Producer producer1 = new Producer(t, "producer1");
		Producer producer2 = new Producer(t, "producer2");
		Producer producer3 = new Producer(t, "producer3");
		producer1.start();
		producer2.start();
		producer3.start();
	
		
	}

}

结果:(不唯一,因为得根据调度算法的策略)
consumer1 say: No food and I will wait
consumer2 say: No food and I will wait
producer1 say: I produce one and now counter is 1
producer3 say: I produce one and now counter is 2
producer2 say: I produce one and now counter is 3
producer1 say: I produce one and now counter is 4
producer3 say: I produce one and now counter is 5
consumer1 say: eat one and now counter is 4
consumer2 say: eat one and now counter is 3
producer2 say: I produce one and now counter is 4
producer1 say: I produce one and now counter is 5
producer3 say: I produce one and now counter is 6
consumer1 say: eat one and now counter is 5
consumer2 say: eat one and now counter is 4
producer2 say: I produce one and now counter is 5
producer1 say: I produce one and now counter is 6
producer3 say: I produce one and now counter is 7
consumer1 say: eat one and now counter is 6
consumer2 say: eat one and now counter is 5
producer2 say: I produce one and now counter is 6
producer1 say: I produce one and now counter is 7
...(此处省略)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值