-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathStack.java
64 lines (55 loc) · 1.06 KB
/
Stack.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package it.polimi.deib.se.ex05.concurrent.stack;
/**
* @author Alessandro Rizzi, Mattia Salnitri
* classe per la gestione di uno stack tread safe
*
*/
public class Stack implements Stack_interface{
/**
* @author Alessandro Rizzi, Mattia Salnitri
* innerclass per la gestione degli elementi dello stack
*
*/
private class Element {
int info;
Element next;
/**
* costruttore della classe
* per una lista linkata
* @param n elemento
* @param e referenza all'elemento precedente
*/
Element(int n, Element e){
info = n;
next = e;
}
}
/**
* primo elemento dello stack
*/
private Element first;
/**
* costruttore
*/
public Stack(){
first = null;
}
public synchronized void push(int n){
first = new Element(n, first);
notifyAll();
}
public synchronized int pop(){
while(first == null){
try {
wait();
} catch (InterruptedException e) {
}
}
Element element = first;
first = first.next;
return element.info;
}
public synchronized boolean is_empty(){
return (first == null);
}
}