主要涉及的内容是对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
...(此处省略)