?? customerandproducter.java
字號(hào):
import java.io.*;
class Market
{
int i;
boolean flag;
public Market()
{
i=1;
flag=false;
}
public synchronized void put(int j)
{
i=j;
if(flag==true)
{
try
{
wait();
}
catch(InterruptedException ie)
{
System.out.println(ie);
}
}
System.out.println("put :"+i);
flag=true;
notify();
}
public synchronized void get()
{
if(flag==false)
{
try
{
wait();
}
catch(InterruptedException ie)
{
System.out.println(ie);
}
}
System.out.println("Get :"+i);
flag=false;
notify();
}
}
class Producter extends Thread
{
int k;
Market m;
public Producter(Market n)
{
k=1;
m=n;
Thread t=new Thread(this);
t.start();
}
public void run()
{
while(true)
{
m.put(k++);
}
}
}
class Customer extends Thread
{
Market m;
public Customer(Market n)
{
m=n;
Thread t=new Thread(this);
t.start();
}
public void run()
{
while(true)
{
m.get();
}
}
}
public class CustomerAndProducter
{
public static void main(String[] args)
{
Market w=new Market();
new Producter(w);
new Customer(w);
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -