|
10 | 10 | import java.util.Map; |
11 | 11 | import java.util.Map.Entry; |
12 | 12 | import java.util.Queue; |
| 13 | +import java.util.concurrent.ArrayBlockingQueue; |
| 14 | +import java.util.concurrent.BlockingQueue; |
13 | 15 | import java.util.concurrent.ConcurrentHashMap; |
14 | 16 | import java.util.concurrent.ConcurrentLinkedQueue; |
15 | 17 | import java.util.concurrent.ExecutorService; |
|
23 | 25 |
|
24 | 26 | public class talk extends AbstractApplication { |
25 | 27 |
|
26 | | - private static final long TIMEOUT = 200; |
| 28 | + private static final long TIMEOUT = 10; |
27 | 29 | private static final int DEFAULT_POOL_SIZE = 3; |
| 30 | + protected static final int DEFAULT_MESSAGE_POOL_SIZE = 10; |
| 31 | + protected final Map<String, BlockingQueue<Builder>> meetings = new ConcurrentHashMap<String, BlockingQueue<Builder>>(); |
28 | 32 | protected final Map<String, Queue<Builder>> list = new ConcurrentHashMap<String, Queue<Builder>>(); |
29 | | - protected final Map<String, Queue<Builder>> meetings = new ConcurrentHashMap<String, Queue<Builder>>(); |
30 | 33 | protected final Map<String, List<String>> sessions = new ConcurrentHashMap<String, List<String>>(); |
31 | 34 | private ExecutorService service; |
32 | 35 |
|
@@ -89,39 +92,38 @@ public String save(Object meetingCode, String sessionId, String message) { |
89 | 92 | * @return builder |
90 | 93 | */ |
91 | 94 | public final String save(final Object meetingCode, final Builder builder) { |
92 | | - final Queue<Builder> messages; |
93 | | - synchronized (this.meetings) { |
94 | | - if (this.meetings.get(meetingCode) == null) { |
95 | | - this.meetings.put(meetingCode.toString(), new ConcurrentLinkedQueue<Builder>()); |
96 | | - } |
| 95 | + BlockingQueue<Builder> messages; |
| 96 | + if ((messages = this.meetings.get(meetingCode)) == null) { |
| 97 | + messages = new ArrayBlockingQueue<Builder>(DEFAULT_MESSAGE_POOL_SIZE); |
| 98 | + this.meetings.put(meetingCode.toString(), messages); |
| 99 | + } |
97 | 100 |
|
98 | | - messages = this.meetings.get(meetingCode); |
99 | | - messages.add(builder); |
100 | | - this.meetings.notifyAll(); |
| 101 | + try { |
| 102 | + messages.put(builder); |
| 103 | + } catch (InterruptedException e) { |
| 104 | + e.printStackTrace(); |
101 | 105 | } |
102 | 106 |
|
103 | 107 | this.getService().execute(new Runnable(){ |
104 | 108 | @Override |
105 | 109 | public void run() { |
106 | | - synchronized(talk.this.meetings) { |
107 | 110 | Builder message; |
108 | 111 | do { |
109 | 112 | try { |
110 | | - talk.this.meetings.wait(TIMEOUT); |
| 113 | + Thread.sleep(TIMEOUT); |
111 | 114 | } catch (InterruptedException e) { |
112 | | - e.printStackTrace(); |
| 115 | + e.printStackTrace(); |
113 | 116 | } |
114 | 117 | } while(talk.this.meetings.get(meetingCode) == null || (message = talk.this.meetings.get(meetingCode).poll()) == null); |
115 | 118 |
|
116 | 119 | talk.this.copy(meetingCode, message); |
117 | | - } |
118 | 120 | } |
119 | 121 | }); |
120 | 122 | return builder.toString(); |
121 | 123 | } |
122 | 124 |
|
123 | 125 | private ExecutorService getService() { |
124 | | - return this.service!=null? this.service : Executors.newFixedThreadPool(DEFAULT_POOL_SIZE); |
| 126 | + return this.service!=null? this.service : Executors.newFixedThreadPool(DEFAULT_POOL_SIZE); |
125 | 127 | } |
126 | 128 |
|
127 | 129 | /** |
@@ -195,7 +197,7 @@ public String version() { |
195 | 197 | * @throws ApplicationException |
196 | 198 | */ |
197 | 199 | public boolean testing(final int n) throws ApplicationException { |
198 | | - this.meetings.put("[M001]", new ConcurrentLinkedQueue<Builder>()); |
| 200 | + this.meetings.put("[M001]", new ArrayBlockingQueue<Builder>(DEFAULT_MESSAGE_POOL_SIZE)); |
199 | 201 | this.list.put("{A}", new ConcurrentLinkedQueue<Builder>()); |
200 | 202 | this.list.put("{B}", new ConcurrentLinkedQueue<Builder>()); |
201 | 203 |
|
|
0 commit comments