Salve ragazzi
Qualcuno potrebbe aiutarmi ad implementare uno Stack Thread Safe-Autobloccante?Io ho provato così ma non so se sia giusto o meno
codice:
public class Stack<Info> {
private LinkedList<Info> s;
private int capacity;
public synchronized int getCapacity() {
return capacity;
}
public Stack(int capacity) {
this.capacity = capacity;
this.s = new LinkedList<Info>();
}
public synchronized boolean isEmpty(){
return s.size() == 0;
}
public synchronized boolean isFull(){
return s.size() == capacity;
}
public synchronized int size(){
return s.size();
}
public synchronized void push(Info i) {
while(isFull()){
try { s.wait();
} catch (InterruptedException ex) { }
}
s.add(i);
s.notifyAll();
}
public synchronized Info pop(){
while(isEmpty()){
try { s.wait();
} catch (InterruptedException ex) { }
}
s.notifyAll();
return s.removeLast();
}
public synchronized void reset(){
s.removeAll(s);
}
}