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