1. The production and consumption of bees and bears, bear in the honey full 10 pounds eaten. Bees produce a catty of honey once, and the time it takes for a bee to produce a pound of honey is 10s.
10 bees and two bears.
package com.yehomlab;import java.util.arraylist;import java.util.list;class bear implements runnable { list<integer> list; public bear (list<integer> list) { this.list=list; }public void run () {while (True) {synchronized (list) {int size=list.size (); size==0) {try {system.out.println ("Honey, Come on, honey! "); list.wait ();} catch (exception e) {}}else if (size>0 && size<10) {try { System.out.println ("Fill 10 I eat again"); list.wait ();} catch (exception e) {}}else if (size==10) {integer i=list.remove (0); System.out.println ("Eat Honey"); List.notifyall ();}} Yield ();}}} class bee implements runnable {final static int max=10; list<integer> list;public bee (list<integer> list) {this.list=List;} Public void run () {int i=1;while (True) {synchronized (list) {int size= List.size (); if (Size>=max) {try {list.wait ();} catch (exception e) {}}else if (Size<max) {List.add (New integer (i)); System.out.println ("producing nectar"); Try {thread.sleep (200);} catch (exception e) {}i+=1;list.notifyall ();}}}} Public class beebeardemo {public static void main (String[] args) {List< Integer> list =new arraylist<integer> (); Bear b1 = new bear (list); Bear b2 = new bear (list); Bee e1 = new bee (list); Bee e2 = new bee (list); Bee e3 = new bee (list); Bee e4 = new bee (list); Bee e5 = new bee (list); Bee e6 = new bee (list); Bee e7 = new bee (list); Bee&nBsp; e8 = new bee (list); Bee e9 = new bee (list); Bee e10= new bee (list); New thread (B1). Start (); New thread (B2). Start ();new Thread (E1). Start (); New thread (E2). Start (); New thread (E3). Start (); New thread (E4). Start (); new thread (E5). Start (), New thread (E6). Start (); New thread (E7). Start (); New thread (E8). Start (); New thread (E9). Start (); New thread (E10). Start ();}}
This article from "Yehomlab" blog, reproduced please contact the author!
The experiment of "DAY9" about Cheng eating honey Demo1 in multi-line