Java thread learning the first day __ Low edition of the bread maker

Source: Internet
Author: User

Package Thread;

Import javax.xml.bind.ValidationEvent;

Class snacks{
private int saledsnacks=0;
private int totalsnacksnum=10; Initialize the snack bar starts with 10 loaves of bread
private int totaltimeofsale=0;
Public synchronized Boolean salesnacks () {
if (totalsnacksnum>0) {
saledsnacks++;
totalsnacksnum--;
totaltimeofsale++;
System.out.println (Thread.CurrentThread (). GetName () + "sell a loaf");
return true;
}else {
System.out.println ("Now sold out, need to wait until");
return false;
}
}
Public synchronized Boolean makesnacks () {
totalsnacksnum++;
totaltimeofsale+=3;
System.out.println (Thread.CurrentThread (). GetName () + "Make a loaf");
return true;
}
}
Class Salesnacks extends thread{
Private snacks snacks=new snacks ();
Public Salesnacks (String name,snacks s) {
Super (name);
Snacks=s;
}

public void Validation () {
while (Snacks.salesnacks ()) {
try {
Thread.Sleep (1000);
} catch (Interruptedexception e) {
}
}
if (! Snacks.salesnacks ()) {
try {
Thread.Sleep (5000);
while (Snacks.salesnacks ()) {
try {
Thread.Sleep (1000);
} catch (Interruptedexception e) {
}
}

} catch (Exception e) {
Todo:handle exception
}

}
Validation ();
}
@Override
public void Run () {
Validation ();
}
}
Class Makesnacks extends thread{
Private snacks snacks=new snacks ();
Public Makesnacks (String name,snacks s) {
Super (name);
Snacks=s;
}
@Override
public void Run () {
while (Snacks.makesnacks ()) {
try {
Thread.Sleep (3000);
} catch (Interruptedexception e) {
}

}
}
}

public class Thread_synchronized {
public static void Main (string[] args) throws Interruptedexception {
System.out.println ("Start selling snack!!! ");
Snacks Aimsnackshop=new Snacks ();
Salesnacks sale1=new salesnacks ("Windows:a", aimsnackshop);
Salesnacks sale2=new salesnacks ("Windows:b", aimsnackshop);
Salesnacks sale3=new salesnacks ("Windows:c", aimsnackshop);
Makesnacks makesnacks =new makesnacks ("Make:a", aimsnackshop);
Sale1.start ();
Sale2.start ();
Sale3.start ();
Makesnacks.start ();
try {
Thread.Sleep (500000);
} catch (Illegalthreadstateexception e) {
E.printstacktrace ();
}
}
}

Java thread learning the first day __ Low edition of the bread maker

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.