| 1 |
|
| 2 |
package org.jgroups.util; |
| 3 |
|
| 4 |
|
| 5 |
import org.jgroups.TimeoutException; |
| 6 |
import org.jgroups.log.Trace; |
| 7 |
|
| 8 |
import java.util.Vector; |
| 9 |
|
| 10 |
import EDU.oswego.cs.dl.util.concurrent.WaitFreeQueue; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
/** |
| 16 |
* Elements are added at the tail and removed from the head. Class is thread-safe in that |
| 17 |
* 1 producer and 1 consumer may add/remove elements concurrently. The class is not |
| 18 |
* explicitely designed for multiple producers or consumers. Implemented as a linked |
| 19 |
* list, so that removal of an element at the head does not cause a right-shift of the |
| 20 |
* remaining elements (as in a Vector-based implementation). |
| 21 |
* @author Bela Ban |
| 22 |
* @author Filip Hanik |
| 23 |
*/ |
| 24 |
public class Queue extends WaitFreeQueue { |
| 25 |
boolean closed=false; |
| 26 |
int size=0; |
| 27 |
|
| 28 |
|
| 29 |
public Queue() { |
| 30 |
} |
| 31 |
|
| 32 |
public Object getFirst() { |
| 33 |
return super.peek(); |
| 34 |
} |
| 35 |
|
| 36 |
public Object getLast() { |
| 37 |
return null; // not implemented |
| 38 |
} |
| 39 |
|
| 40 |
public boolean closed() { |
| 41 |
return closed; |
| 42 |
} |
| 43 |
|
| 44 |
public void add(Object obj) throws QueueClosedException { |
| 45 |
try { |
| 46 |
super.put(obj); |
| 47 |
size++; |
| 48 |
} |
| 49 |
catch(InterruptedException e) { |
| 50 |
e.printStackTrace(); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
public void addAtHead(Object obj) throws QueueClosedException { |
| 55 |
try { |
| 56 |
super.put(obj); |
| 57 |
size++; |
| 58 |
} |
| 59 |
catch(InterruptedException e) { |
| 60 |
e.printStackTrace(); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
public Object remove() throws QueueClosedException { |
| 65 |
Object retval=null; |
| 66 |
try { |
| 67 |
retval=super.take(); |
| 68 |
size--; |
| 69 |
return retval; |
| 70 |
} |
| 71 |
catch(InterruptedException e) { |
| 72 |
e.printStackTrace(); |
| 73 |
return retval; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
public Object remove(long timeout) throws QueueClosedException, TimeoutException { |
| 78 |
return remove(); |
| 79 |
} |
| 80 |
|
| 81 |
public void removeElement(Object obj) throws QueueClosedException { |
| 82 |
; |
| 83 |
} |
| 84 |
|
| 85 |
public Object peek() { |
| 86 |
return super.peek(); |
| 87 |
} |
| 88 |
|
| 89 |
public Object peek(long timeout) throws QueueClosedException, TimeoutException { |
| 90 |
return peek(); |
| 91 |
} |
| 92 |
|
| 93 |
public void close(boolean flush_entries) { |
| 94 |
closed=true; |
| 95 |
} |
| 96 |
|
| 97 |
public void reset() { |
| 98 |
closed=false; |
| 99 |
size=0; |
| 100 |
} |
| 101 |
|
| 102 |
public int size() { |
| 103 |
return size; |
| 104 |
} |
| 105 |
|
| 106 |
public String toString() { |
| 107 |
return super.toString(); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
} |