The experiment of "DAY9" about Cheng eating honey Demo1 in multi-line

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.