class Queue
{
int i;
boolean isfull = false; public synchronized void put(int i)
{
if (!isfull)
{ this.i = i;
isfull = true;
notify(); }
try
{
wait();
} catch (Exception e)
{
e.printStackTrace();
}
} public synchronized int get()
{
if (!isfull)
{
try
{
wait();
} catch (Exception e)
{
e.printStackTrace();
}
}
isfull = false;
notify();
return i;
}
}class Producer extends Thread
{
Queue q; Producer(Queue q)
{
this.q = q;
} public void run()
{
for (int i = 0; i <1; i++)
{
q.put(i);
System.out.println(i);
}
}
}class Consumer extends Thread
{
Queue q; Consumer(Queue q)
{
this.q = q;
} public void run()
{ while (true)
{
System.out.println(q.get()); }
}
}public class WaitTest
{
public static void main(String[] args)
{
Queue q = new Queue();
Producer p = new Producer(q);
Consumer c = new Consumer(q);
p.run();
c.run(); }
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货