import java.util.Vector;
class Consumer extends Thread
{
private SyncStack theStack;
private int num;
private int counter = 1; public Consumer(SyncStack s)
{
theStack = s;
num = counter ++; }
public void run()
{
char c; for (int i=0;i<200;i++)
{
c=theStack.pop();
System.out.println("Producer"+num+":"+c);
try
{
Thread.sleep(300); }
catch (InterruptedException e)
{
}
}
}
}class Producer implements Runnable
{
private SyncStack theStack;
private int num;
private int counter = 1; public Producer(SyncStack s)
{
theStack = s;
num = counter ++; }
public void run()
{
char c; for (int i=0;i<200;i++)
{
c = (char)(Marh.random()*26+'A');
theStack.push(c);
System.out.println("Producer"+num+":"+c);
try
{
Thread.sleep(300); }
catch (InterruptedException e)
{
}
}
}
}class SyncStack
{
public Vector buffer = new Vector(400,200);
public syschronized char pop()
{
char c;
while(buffer.size==0)
{
try
{
this.wait();
}
catch (InterruptedException e)
{
}
}
c==((Character)buffer.remove(buffer.size()-1)).charValue();
reture c;
}
public syschronized char push(char c)
{
this.notify; Character charObj = new Character(c);
buffer.addElement(charObj);
}
}public class SyncTest
{
public static void main(String[] args)
{
SyncStack stack = new SyncStack(); Producer p1 =new Producer(stack);
Thread prodT1 = new Thread(p1);
prodT1.start(); Producer p2 =new Producer(stack);
Thread prodT2 = new Thread(p2);
prodT2.start(); Consumer c1 =new Consumer(stack);
Thread consT1 = new Thread(c1);
consT1.start(); Consumer c2 =new Consumer(stack);
Thread consT2 = new Thread(c2);
consT2.start();
}
}
class Consumer extends Thread
{
private SyncStack theStack;
private int num;
private int counter = 1; public Consumer(SyncStack s)
{
theStack = s;
num = counter ++; }
public void run()
{
char c; for (int i=0;i<200;i++)
{
c=theStack.pop();
System.out.println("Producer"+num+":"+c);
try
{
Thread.sleep(300); }
catch (InterruptedException e)
{
}
}
}
}class Producer implements Runnable
{
private SyncStack theStack;
private int num;
private int counter = 1; public Producer(SyncStack s)
{
theStack = s;
num = counter ++; }
public void run()
{
char c; for (int i=0;i<200;i++)
{
c = (char)(Marh.random()*26+'A');
theStack.push(c);
System.out.println("Producer"+num+":"+c);
try
{
Thread.sleep(300); }
catch (InterruptedException e)
{
}
}
}
}class SyncStack
{
public Vector buffer = new Vector(400,200);
public syschronized char pop()
{
char c;
while(buffer.size==0)
{
try
{
this.wait();
}
catch (InterruptedException e)
{
}
}
c==((Character)buffer.remove(buffer.size()-1)).charValue();
reture c;
}
public syschronized char push(char c)
{
this.notify; Character charObj = new Character(c);
buffer.addElement(charObj);
}
}public class SyncTest
{
public static void main(String[] args)
{
SyncStack stack = new SyncStack(); Producer p1 =new Producer(stack);
Thread prodT1 = new Thread(p1);
prodT1.start(); Producer p2 =new Producer(stack);
Thread prodT2 = new Thread(p2);
prodT2.start(); Consumer c1 =new Consumer(stack);
Thread consT1 = new Thread(c1);
consT1.start(); Consumer c2 =new Consumer(stack);
Thread consT2 = new Thread(c2);
consT2.start();
}
}
Vector类的设计是线程安全的(它的remove,add是同步方法)import java.util.Vector;
class Consumer extends Thread {
private SyncStack theStack;
private int num;
static private int counter = 1; //不同对象共享一个
public Consumer(SyncStack s) {
theStack = s;
num = counter ++;
}
public void run() {
char c;
for(int i=0; i<200; i++) {
c=theStack.pop();
System.out.println("Consumer"+num+":"+c);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
}
}
}
} class Producer implements Runnable {
private SyncStack theStack;
private int num;
static private int counter = 1; //static added
public Producer(SyncStack s) {
theStack = s;
num = counter++;
}
public void run() {
char c;
for( int i=0; i<200; i++) {
c = (char)(Math.random()*26+'A');
System.out.println("Producer"+num+":"+c);
theStack.push(c);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
}
}
}
} class SyncStack {
public Vector<Character> buffer = new Vector<Character>(400,200);
public synchronized char pop() {
char c;
while(buffer.size()==0) {
try {
this.wait();
} catch (InterruptedException e) {
}
}
c=((Character)buffer.remove(buffer.size()-1)).charValue();
return c;
}
public synchronized void push(char c) {
this.notify();
Character charObj = new Character(c);
buffer.addElement(charObj);
}
}public class SyncTest {
public static void main(String[] args){
SyncStack stack = new SyncStack();
Producer p1 =new Producer(stack);
Thread prodT1 = new Thread(p1);
prodT1.start();
Producer p2 =new Producer(stack);
Thread prodT2 = new Thread(p2);
prodT2.start();
Consumer c1 =new Consumer(stack);
Thread consT1 =new Thread(c1);
consT1.start();
Consumer c2 =new Consumer(stack);
Thread consT2 = new Thread(c2);
consT2.start();
}
}