/** 堆栈 */
class Stack {
private String name;
private String[] buffer=new String[100];
int point=-1;
public Stack(String name){this.name=name;}
public String getName(){return name;}
public synchronized int getPoint(){return point;}
public synchronized String pop() {
this.notifyAll();
while(point==-1){
System.out.println(Thread.currentThread().getName()+": wait");
try{
this.wait();
}catch(InterruptedException e){throw new RuntimeException(e);}
}
String goods = buffer[point];
buffer[point]=null;
Thread.yield();
point--;
return goods;
}
public synchronized void push(String goods) {
this.notifyAll();
while(point==buffer.length-1){
System.out.println(Thread.currentThread().getName()+": wait");
try{
this.wait();
}catch(InterruptedException e){throw new RuntimeException(e);}
}
point++;
Thread.yield();
buffer[point]=goods;
}
}
我看了数据结构觉得排序查找,递归都还有用,不知道栈和堆,还有队列有什么用,一直领悟不到精华 |
|