KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > threadpool > impl > Queue


1 package org.exoplatform.services.threadpool.impl;
2 /**
3  * $Id: Queue.java,v 1.2 2004/05/24 17:02:00 tuan08 Exp $
4  *
5  * The contents of this file are subject to the ClickBlocks Public
6  * License Version 1.0 (the "License"); you may not use this file
7  * except in compliance with the License. You may obtain a copy of
8  * the License at http://www.clickblocks.org
9  *
10  * Software distributed under the License is distributed on an "AS
11  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
12  * implied, including, but not limited to, the implied warranties of
13  * merchantability, fitness for a particular purpose and
14  * non-infringement. See the License for the specific language
15  * governing rights and limitations under the License.
16  *
17  * ClickBlocks, the ClickBlocks logo and combinations thereof are
18  * trademarks of ClickBlocks, LLC in the United States and other
19  * countries.
20  *
21  * The Initial Developer of the Original Code is ClickBlocks, LLC.
22  * Portions created by ClickBlocks, LLC are Copyright (C) 2000.
23  * All Rights Reserved.
24  *
25  * Contributor(s): Mark Grand
26  */

27
28 import java.util.ArrayList JavaDoc;
29
30 public class Queue {
31   private ArrayList JavaDoc data = new ArrayList JavaDoc();
32   private int maxQueueSize = Integer.MAX_VALUE;
33
34   /**
35    * Put an object on the end of the queue. If the size of
36    * the queue is equal to or greater than the current value
37    * of maxQueueSize then this method will wait until the
38    * size of the queue shrinks to less than maxQueueSize.
39    *
40    * @param obj the object to put at end of queue
41    */

42   synchronized public void put(Object JavaDoc obj) throws InterruptedException JavaDoc {
43     if (Thread.currentThread().isInterrupted()) throw new InterruptedException JavaDoc();
44     if (obj == null) throw new IllegalArgumentException JavaDoc("null");
45     while (data.size() >= maxQueueSize) {
46       try {
47         wait();
48       } catch (InterruptedException JavaDoc e) {
49         return;
50       } // try
51
} // while
52
data.add(obj);
53     notify();
54   } // put(Object)
55

56   /**
57    * Put an object on the end of the queue. If the size of
58    * the queue is equal to or greater than the current value
59    * of maxQueueSize then this method will wait until the
60    * size of the queue shrinks to less than maxQueueSize.
61    *
62    * @param obj the object to put at end of queue
63    * @param msecs If this method has to wait for the size of the
64    * queue to shrink to less than maxQueueSize, it
65    * will stop waiting after it has waited this many
66    * milliseconds.
67    * @return false if this method returns without queuing the
68    * given object becuase it had to wait msec
69    * milliseconds; otherwise true.
70    */

71   synchronized public boolean put(Object JavaDoc obj, long msecs) throws InterruptedException JavaDoc {
72     if (Thread.currentThread().isInterrupted()) throw new InterruptedException JavaDoc();
73     if (obj == null) throw new IllegalArgumentException JavaDoc("null");
74     long startTime = System.currentTimeMillis();
75     long waitTime = msecs;
76     while (data.size() >= maxQueueSize) {
77       waitTime = msecs - (System.currentTimeMillis() - startTime);
78       if (waitTime <= 0) return false;
79       wait(waitTime);
80     } // while
81
data.add(obj);
82     notify();
83     return true;
84   } // put(Object, long)
85

86   /**
87    * Get an object from the front of the queue.
88    * If queue is empty, waits until it is not empty.
89    */

90   synchronized public Object JavaDoc get() throws InterruptedException JavaDoc {
91     while (data.size() == 0) {
92       wait();
93     } // while
94
Object JavaDoc obj = data.remove(0);
95     notify();
96     return obj;
97   } // get()
98

99   /**
100    * Get an object from the front of the queue.
101    * If queue is empty, waits until it is not empty.
102    *
103    * @param msecs The maximum number of milliseconds that this
104    * method should wait before giving up.
105    * @return The object at the front of the queue or null if
106    * there are no objects in the queue and the method
107    * has waited at least the given number of
108    * milliseconds for an object to be put in the
109    * queue.
110    */

111   synchronized public Object JavaDoc get(long msecs) throws InterruptedException JavaDoc {
112     long startTime = System.currentTimeMillis();
113     long waitTime = msecs;
114
115     if (data.size() > 0) return data.remove(0);
116     while (true) {
117       waitTime = msecs - (System.currentTimeMillis() - startTime);
118       if (waitTime <= 0) return null;
119       wait(waitTime);
120       if (data.size() > 0) {
121         Object JavaDoc obj = data.remove(0);
122         notify();
123         return obj;
124       } // if data.size()
125
} // while
126
} // get(long)
127

128   /**
129    * Set the maximum queue size.
130    */

131   public void setMaxQueueSize(int newValue) {
132     maxQueueSize = newValue;
133   }
134 }
Popular Tags