The Producer-Consumer Relationship

//Listing 3-1. The Producer-Consumer Relationship Version 1
public class PC
{
public static void main(String[] args)
{
Shared s = new Shared();
new Producer(s).start();
new Consumer(s).start();
}
} class Shared
{
private char c;
private volatile boolean writeable = true;
synchronized void setSharedChar(char c)
{
while (!writeable)
try
{
wait();
}
catch (InterruptedException ie)
{
}
this.c = c;
writeable = false;
notify();
}
synchronized char getSharedChar()
{
while (writeable)
try
{
wait();
}
catch (InterruptedException ie)
{
}
writeable = true;
notify();
return c;
}
} class Producer extends Thread
{
private final Shared s;
Producer(Shared s)
{
this.s = s;
}
@Override
public void run()
{
for (char ch = 'A'; ch <= 'Z'; ch++)
{
s.setSharedChar(ch);
System.out.println(ch + " produced by producer.");
}
}
}
class Consumer extends Thread
{
private final Shared s;
Consumer(Shared s)
{
this.s = s;
}
@Override
public void run()
{
char ch;
do
{
ch = s.getSharedChar();
System.out.println(ch + " consumed by consumer.");
}
while (ch != 'Z');
}
}
上一篇:狗日的js的闭包


下一篇:在 Windows 下远程桌面连接 Linux - XManager 篇