gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.fmap / src / main / java / org / gvsig / raster / util / Queue.java @ 2443
History | View | Annotate | Download (2.19 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.raster.util; |
23 |
|
24 |
import java.util.Vector; |
25 |
|
26 |
/**
|
27 |
* A simple FIFO queue class which causes the calling thread to wait if the
|
28 |
* queue is empty and notifies threads that are waiting when it is not
|
29 |
* empty.
|
30 |
*
|
31 |
* @author Anil V (akv@eng.sun.com)
|
32 |
*/
|
33 |
public class Queue { |
34 |
private Vector<Object> vector = new Vector<Object>(); |
35 |
|
36 |
/**
|
37 |
* Put the object into the queue.
|
38 |
*
|
39 |
* @param object
|
40 |
* the object to be appended to the queue.
|
41 |
*/
|
42 |
public synchronized void put(Object object) { |
43 |
vector.addElement(object); |
44 |
notify(); |
45 |
} |
46 |
|
47 |
/**
|
48 |
* Pull the first object out of the queue. Wait if the queue is empty.
|
49 |
*/
|
50 |
public synchronized Object pull() { |
51 |
while (isEmpty())
|
52 |
try {
|
53 |
wait(); |
54 |
} catch (InterruptedException ex) { |
55 |
} |
56 |
return get();
|
57 |
} |
58 |
|
59 |
/**
|
60 |
* Get the first object out of the queue. Return null if the queue is
|
61 |
* empty.
|
62 |
*/
|
63 |
public synchronized Object get() { |
64 |
Object object = peek();
|
65 |
if (object != null) |
66 |
vector.removeElementAt(0);
|
67 |
return object;
|
68 |
} |
69 |
|
70 |
/**
|
71 |
* Peek to see if something is available.
|
72 |
*/
|
73 |
public Object peek() { |
74 |
if (isEmpty())
|
75 |
return null; |
76 |
return vector.elementAt(0); |
77 |
} |
78 |
|
79 |
/**
|
80 |
* Is the queue empty?
|
81 |
*/
|
82 |
public boolean isEmpty() { |
83 |
return vector.isEmpty();
|
84 |
} |
85 |
|
86 |
/**
|
87 |
* How many elements are there in this queue?
|
88 |
*/
|
89 |
public int size() { |
90 |
return vector.size();
|
91 |
} |
92 |
} |