黑马程序员技术交流社区
标题:
用java模拟的栈和堆不明白它具体有什么用
[打印本页]
作者:
王章亚
时间:
2012-7-3 14:18
标题:
用java模拟的栈和堆不明白它具体有什么用
/** 堆栈 */
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;
}
}
我看了数据结构觉得排序查找,递归都还有用,不知道栈和堆,还有队列有什么用,一直领悟不到精华
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2