8 |
|
import java.util.*; |
9 |
|
|
10 |
|
/** |
11 |
< |
* A Queue in which each put must wait for a take, and vice versa. |
12 |
< |
* SynchronousQueues are similar to rendezvous channels used in CSP |
13 |
< |
* and Ada. They are well suited for handoff designs, in which an |
14 |
< |
* object running in one thread must synch up with an object running |
15 |
< |
* in another thread in order to hand it some information, event, or |
16 |
< |
* task. |
11 |
> |
* A {@link Queue} in which each put must wait for a take, and vice |
12 |
> |
* versa. SynchronousQueues are similar to rendezvous channels used |
13 |
> |
* in CSP and Ada. They are well suited for handoff designs, in which |
14 |
> |
* an object running in one thread must synch up with an object |
15 |
> |
* running in another thread in order to hand it some information, |
16 |
> |
* event, or task. |
17 |
|
**/ |
18 |
|
public class SynchronousQueue<E> extends AbstractQueue<E> |
19 |
|
implements BlockingQueue<E>, java.io.Serializable { |
285 |
|
|
286 |
|
public SynchronousQueue() {} |
287 |
|
|
288 |
– |
public boolean isEmpty() { |
289 |
– |
return true; |
290 |
– |
} |
291 |
– |
|
292 |
– |
public int size() { |
293 |
– |
return 0; |
294 |
– |
} |
295 |
– |
|
296 |
– |
/** |
297 |
– |
* Always returns zero. SynchronousQueues have no internal capacity. |
298 |
– |
* @return zero. |
299 |
– |
*/ |
300 |
– |
public int remainingCapacity() { |
301 |
– |
return 0; |
302 |
– |
} |
303 |
– |
|
304 |
– |
public E peek() { |
305 |
– |
return null; |
306 |
– |
} |
307 |
– |
|
288 |
|
|
289 |
|
public void put(E x) throws InterruptedException { |
290 |
|
doPut(x, false, 0); |
349 |
|
} |
350 |
|
} |
351 |
|
|
352 |
< |
public boolean remove(Object x) { |
353 |
< |
return false; |
352 |
> |
/** |
353 |
> |
* Always returns true. SynchronousQueues have no internal capacity. |
354 |
> |
* @return true. |
355 |
> |
*/ |
356 |
> |
public boolean isEmpty() { |
357 |
> |
return true; |
358 |
> |
} |
359 |
> |
|
360 |
> |
/** |
361 |
> |
* Always returns 0. SynchronousQueues have no internal capacity. |
362 |
> |
* @return zero. |
363 |
> |
*/ |
364 |
> |
public int size() { |
365 |
> |
return 0; |
366 |
|
} |
367 |
|
|
368 |
< |
static class EmptyIterator<E> implements Iterator { |
368 |
> |
/** |
369 |
> |
* Always returns zero. SynchronousQueues have no internal capacity. |
370 |
> |
* @return zero. |
371 |
> |
*/ |
372 |
> |
public int remainingCapacity() { |
373 |
> |
return 0; |
374 |
> |
} |
375 |
> |
|
376 |
> |
/** |
377 |
> |
* Always returns null. SynchronousQueues do not return elements |
378 |
> |
* unless actively waited on. |
379 |
> |
* @return null. |
380 |
> |
*/ |
381 |
> |
public E peek() { |
382 |
> |
return null; |
383 |
> |
} |
384 |
> |
|
385 |
> |
|
386 |
> |
static class EmptyIterator<E> implements Iterator<E> { |
387 |
|
public boolean hasNext() { |
388 |
|
return false; |
389 |
|
} |
395 |
|
} |
396 |
|
} |
397 |
|
|
398 |
+ |
/** |
399 |
+ |
* Returns an empty iterator. |
400 |
+ |
*/ |
401 |
|
public Iterator<E> iterator() { |
402 |
< |
return new EmptyIterator(); |
402 |
> |
return new EmptyIterator<E>(); |
403 |
|
} |
404 |
|
|
405 |
|
|
406 |
+ |
/** |
407 |
+ |
* Returns an empty array. |
408 |
+ |
*/ |
409 |
|
public Object[] toArray() { |
410 |
|
return new E[0]; |
411 |
|
} |