1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.mina.util;
20
21 import java.util.HashSet;
22 import java.util.Iterator;
23
24 /***
25 * A {@link HashSet} that can wait until it is not empty.
26 *
27 * @author Trustin Lee (trustin@apache.org)
28 * @version $Rev: 210062 $, $Date: 2005-07-11 12:52:38 +0900 $
29 */
30 public class BlockingSet extends HashSet
31 {
32 private static final long serialVersionUID = 3258134669538309941L;
33
34 private int waiters = 0;
35
36 public synchronized boolean add( Object o )
37 {
38 boolean ret = super.add( o );
39 if( ret && waiters > 0 )
40 notify();
41 return ret;
42 }
43
44 public Iterator iterator()
45 {
46 return super.iterator();
47 }
48
49 public synchronized boolean remove( Object o )
50 {
51 return super.remove( o );
52 }
53
54 public synchronized void waitForNewItem() throws InterruptedException
55 {
56 waiters++;
57 try
58 {
59 while( isEmpty() )
60 {
61 wait();
62 }
63 }
64 finally
65 {
66 waiters--;
67 }
68 }
69 }