KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > util > SynchronizedQueue


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: SynchronizedQueue.java,v 1.4 2007/01/07 06:14:00 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.util;
23
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
26
27 /**
28  * Class that implement unlimited queue, that is synchronized. It means that
29  * the consumer of the objects from the queue waits/is blocked in the get
30  * method until there is an object available.
31  *
32  * @version $Id: SynchronizedQueue.java,v 1.4 2007/01/07 06:14:00 bastafidli Exp $
33  * @author Miro Halas
34  * @code.reviewer Miro Halas
35  * @code.reviewed Initial revision
36  */

37 public class SynchronizedQueue
38 {
39    // Attributes ///////////////////////////////////////////////////////////////
40

41    /**
42     * Cache of object produced by producer and consumed by consumer.
43     */

44    protected List JavaDoc m_lstObjects;
45
46    // Constructors /////////////////////////////////////////////////////////////
47

48    /**
49     * Constructor for Synchronized Queue Object.
50     */

51    public SynchronizedQueue(
52    )
53    {
54       super();
55
56       m_lstObjects = new LinkedList JavaDoc();
57    }
58
59    // Logic ////////////////////////////////////////////////////////////////////
60

61    /**
62     * Destructor for Synchronized Queue. It is called when no other
63     * object holds reference to it.
64     *
65     * @exception Throwable - default destructor exception
66     */

67    protected void finalize(
68    ) throws Throwable JavaDoc
69    {
70       // Explicitely remove this just to help garbage collector
71
m_lstObjects.clear();
72       m_lstObjects = null;
73
74       super.finalize();
75    }
76
77    /**
78     * Get the object from the beginning of the queue
79     *
80     * @return Object - object from the queue, if the thread is blocked in this
81     * function and you call interrupt method, an InterruptedException
82     * will be thrown.
83     * @exception InterruptedException - if the thread is blocked in this
84     * function and you call interrupt method,
85     * an InterruptedException will be thrown.
86     */

87    public synchronized Object JavaDoc get(
88    ) throws InterruptedException JavaDoc
89    {
90       Object JavaDoc objReturn = null;
91
92       if (m_lstObjects.isEmpty())
93       {
94          // There is no object in the queue, go to sleep
95
try
96          {
97             wait();
98          }
99          catch (InterruptedException JavaDoc ieException)
100          {
101             // Somebody woke us up, that means all threads waiting on this
102
// object competed for the lock and this one won and the object is
103
// locked again
104
// The thread can be woken up in two conditions, producer put new
105
// object into the queue or somebody called interrupt - to interrupt
106
// the wait - in this case rethrow an exception
107
if (m_lstObjects.isEmpty())
108             {
109                throw ieException;
110             }
111          }
112       }
113
114       // Remove the first object in the queue
115
objReturn = m_lstObjects.remove(0);
116
117       return objReturn;
118    }
119
120    /**
121     * Put the object to the end of the queue.
122     *
123     * @param objNew - new object, can be null
124     */

125    public synchronized void put(
126       Object JavaDoc objNew
127    )
128    {
129       m_lstObjects.add(objNew);
130       // New object in the queue, notify others
131
notifyAll();
132    }
133
134    /**
135     * Test if the queue is empty.
136     *
137     * @return boolean - true if the queue is empty
138     */

139    public synchronized boolean isEmpty(
140    )
141    {
142       return m_lstObjects.isEmpty();
143    }
144 }
145
Popular Tags