Bees produce honey.

Source: Internet
Author: User

Scene:

There are several bees and some bears a honeypot each bee has a bag.

Bees produce honey every time 10ms bears eat honey in the Honeypot

Maximum capacity of a honeypot of 30 bags is 20

Bees put every honey in the bag when the honey in the bag is more than 5 o'clock, you can add honey to the honeypot.

/**

* Bees with a capacity of 5 capsules per production 1 time consuming 10ms, 5 of the time to add to the Honeypot

*/

public class Bee extends thread{

private int bag = 0;

private static final int bag_max = 20;

private static final int ONCE = 5; 5 pounds per production can be put into the honeypot

private static final int time = 10;//production one catty cost 10ms

Private box box;

private String name;

Public Bee (String name, box box) {

THIS.name = name;

This.box = box;

}

public void Run () {

while (true) {

If less than 5

if (bag >=0 && Bag < 5) {

bag++;

try {

Thread.Sleep (10);

} catch (Exception e) {}

}

Satisfy the condition of putting honey

if (Bag >= 5) {

Synchronized (box) {

Remove the current honeypot capacity

int cap = box.capacity;

The honeypot is full.

if (Cap >= Box.max) {

Box.notifyall (); Tell the bear to eat honey

}else{

The remaining space in the Honeypot

int remain = Box.max-cap;

If the amount in the package is more than the tank, fill the jar.

if (bag >= remain) {

box.capacity = Box.max;

bag = Bag-remain;

System.out.println (name+ "added =" +remain + "honeypot Current Volume:" +box.capacity);

Box.notifyall ();

}

Insufficient remain

else{

box.capacity = box.capacity + bag;

System.out.println (name+ "added =" +bag + "honeypot Current Volume:" +box.capacity);

Bag = 0;

}

}

}

}

Add honey to the packet, this part of the code does not need synchronization is done concurrently.

if (bag >= Bee.bag_max) {

Synchronized (box) {

try {

Box.wait ();

} catch (Exception e) {}

}

}else{

Bag + +;

System.out.println (name+ "bag =" +bag);

try {

Thread.Sleep (10);

} catch (Exception e) {

Todo:handle exception

}

}

}

}

}

/**

* Bear

*/

public class Bear extends thread{

Private box box;

private String name;

Public Bear (Box box,string name) {

This.box = box;

THIS.name = name;

}

public void Run () {

while (true) {

Synchronized (box) {

if (box.capacity = = box. MAX) {

int tmp = box.capacity;

box.capacity = 0;

SYSTEM.OUT.PRINTLN (name + ":" + "ate" +tmp);

try {

Thread.Sleep (1000);

} catch (Interruptedexception e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

Box.notifyall ();

}else{

try {

Box.wait ();

} catch (Interruptedexception e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

}

}

}

}

}

/**

* Honeypot

*/

public class Box {

public static final int MAX = 30;

public int capacity = 0;

}

/**

* Bee production and consumption problems

*/

public class ThreadDemo3 {

public static void Main (string[] args) {

Box box = new box ();

Bee bee1 = new Bee ("b-1", Box);

Bee bee2 = new Bee ("b-2", Box);

Bee bee3 = new Bee ("b-3", Box);

Bee bee4 = new Bee ("b-4", Box);

Bee bee5 = new Bee ("b-5", Box);

Bee bee6 = new Bee ("b-6", Box);

Bee Bee7 = new Bee ("b-6", Box);

Bee bee8 = new Bee ("b-6", Box);

Bee bee9 = new Bee ("b-6", Box);

Bee bee10 = new Bee ("b-6", Box);

Bears bear1 = new bear (box, "Xiong da");

Bears bear2 = new bear (box, "Xiong II");

Bee1.start ();

Bee2.start ();

Bee3.start ();

Bee4.start ();

Bee5.start ();

Bee6.start ();

Bee7.start ();

Bee8.start ();

Bee9.start ();

Bee10.start ();

Bear1.start ();

Bear2.start ();

}


Bees produce honey.

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.