5.17 JAVA

Source: Internet
Author: User

Class computer{
int num=0;
Boolean flag=false;
Public synchronized void set () {
if (flag) {
try{
Super.wait ();
}catch (Interruptedexception e) {
E.printstacktrace ();
}
}
try{
Thread.Sleep (1000);
}catch (Interruptedexception e) {
E.printstacktrace ();
}
num++;
System.out.println ("Production of the first +num+" computer. ");
Flag=true;
Super.notify ();
}
Public synchronized void get () {
if (!flag) {
try{
Super.wait ();
}catch (Interruptedexception e) {
E.printstacktrace ();
}
}
try{
Thread.Sleep (1000);
}catch (Interruptedexception e) {
E.printstacktrace ();
}
System.out.println ("took the first" +num+ "computer. ");
Flag=false;
Super.notify ();
}
}
Class Customer implements runnable{
Private computer com;

public void Setcompu (computer compu) {
This.com=compu;
}
Public Computer Getcompu () {
return this.com;
}
Public Customer (computer com) {
this.com=com;
}
public void Run () {
for (int i=0;i<10;i++) {
Com.set ();
Com.get ();
}
}
}
public class Test9_2 {
public static void Main (string[] args) {
Computer a=new computer ();
Customer Cus=new Customer (a);
New Thread (cus). Start ();
}
}

5.17 JAVA

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.