Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MTQueue |
|
| 2.3333333333333335;2.333 |
1 | /* | |
2 | * Copyright 1999-2004 The Apache Software Foundation. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | package org.apache.commons.threadpool; | |
17 | ||
18 | import java.util.LinkedList; | |
19 | ||
20 | import org.apache.commons.logging.Log; | |
21 | import org.apache.commons.logging.LogFactory; | |
22 | ||
23 | /** | |
24 | * A multithreaded blocking queue which is very useful for | |
25 | * implementing producer-consumer style threading patterns. | |
26 | * <p> | |
27 | * Multiple blocking threads can wait for items being added | |
28 | * to the queue while other threads add to the queue. | |
29 | * <p> | |
30 | * Non blocking and timout based modes of access are possible as well. | |
31 | * | |
32 | * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> | |
33 | * @version $Revision: 155472 $ | |
34 | */ | |
35 | public class MTQueue { | |
36 | ||
37 | /** The Log to which logging calls will be made. */ | |
38 | 0 | private Log log = LogFactory.getLog(MTQueue.class); |
39 | ||
40 | ||
41 | 0 | private LinkedList list = new LinkedList(); |
42 | 0 | private long defaultTimeout = 10000; |
43 | ||
44 | 0 | public MTQueue() { |
45 | 0 | } |
46 | ||
47 | /** | |
48 | * Returns the current number of object in the queue | |
49 | */ | |
50 | public synchronized int size() { | |
51 | 0 | return list.size(); |
52 | } | |
53 | ||
54 | /** | |
55 | * adds a new object to the end of the queue. | |
56 | * At least one thread will be notified. | |
57 | */ | |
58 | public synchronized void add(Object object) { | |
59 | 0 | list.add( object ); |
60 | 0 | notify(); |
61 | 0 | } |
62 | ||
63 | /** | |
64 | * Removes the first object from the queue, blocking until one is available. | |
65 | * Note that this method will never return null and could block forever. | |
66 | */ | |
67 | public synchronized Object remove() { | |
68 | while (true) { | |
69 | 0 | Object answer = removeNoWait(); |
70 | 0 | if ( answer != null ) { |
71 | 0 | return answer; |
72 | } | |
73 | try { | |
74 | 0 | wait( defaultTimeout ); |
75 | } | |
76 | 0 | catch (InterruptedException e) { |
77 | 0 | log.error( "Thread was interrupted: " + e, e ); |
78 | 0 | } |
79 | 0 | } |
80 | } | |
81 | ||
82 | /** | |
83 | * Removes the first object from the queue, blocking only up to the given | |
84 | * timeout time. | |
85 | */ | |
86 | public synchronized Object remove(long timeout) { | |
87 | 0 | Object answer = removeNoWait(); |
88 | 0 | if (answer == null) { |
89 | try { | |
90 | 0 | wait( timeout ); |
91 | } | |
92 | 0 | catch (InterruptedException e) { |
93 | 0 | log.error( "Thread was interrupted: " + e, e ); |
94 | 0 | } |
95 | 0 | answer = removeNoWait(); |
96 | } | |
97 | 0 | return answer; |
98 | } | |
99 | ||
100 | /** | |
101 | * Removes the first object from the queue without blocking. | |
102 | * This method will return immediately with an item from the queue or null. | |
103 | * | |
104 | * @return the first object removed from the queue or null if the | |
105 | * queue is empty | |
106 | */ | |
107 | public synchronized Object removeNoWait() { | |
108 | 0 | if ( ! list.isEmpty() ) { |
109 | 0 | return list.removeFirst(); |
110 | } | |
111 | 0 | return null; |
112 | } | |
113 | ||
114 | } |