Skip to content

Commit 54798ce

Browse files
committed
Added a monitor object to guarantee the data synchronization
1 parent 03e98c7 commit 54798ce

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed

src/tinystruct/examples/talk.java

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525

2626
public class talk extends AbstractApplication {
2727

28-
private static final long TIMEOUT = 10;
29-
private static final int DEFAULT_POOL_SIZE = 3;
28+
private static final long TIMEOUT = 100;
3029
protected static final int DEFAULT_MESSAGE_POOL_SIZE = 10;
3130
protected final Map<String, BlockingQueue<Builder>> meetings = new ConcurrentHashMap<String, BlockingQueue<Builder>>();
3231
protected final Map<String, Queue<Builder>> list = new ConcurrentHashMap<String, Queue<Builder>>();
3332
protected final Map<String, List<String>> sessions = new ConcurrentHashMap<String, List<String>>();
3433
private ExecutorService service;
34+
private final Object monitor = new Object();
3535

3636
@Override
3737
public void init() {
@@ -42,21 +42,21 @@ public void init() {
4242

4343
if (this.service != null) {
4444
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
45-
@Override
46-
public void run() {
47-
service.shutdown();
48-
while (true) {
49-
try {
50-
System.out.println("Waiting for the service to terminate...");
51-
if (service.awaitTermination(5, TimeUnit.SECONDS)) {
52-
System.out.println("Service will be terminated soon.");
53-
break;
54-
}
55-
} catch (InterruptedException e) {
56-
e.printStackTrace();
57-
}
58-
}
45+
@Override
46+
public void run() {
47+
service.shutdown();
48+
while (true) {
49+
try {
50+
System.out.println("Waiting for the service to terminate...");
51+
if (service.awaitTermination(5, TimeUnit.SECONDS)) {
52+
System.out.println("Service will be terminated soon.");
53+
break;
54+
}
55+
} catch (InterruptedException e) {
56+
e.printStackTrace();
57+
}
5958
}
59+
}
6060
}));
6161
}
6262
}
@@ -115,7 +115,7 @@ public void run() {
115115
}
116116

117117
private ExecutorService getService() {
118-
return this.service!=null? this.service : Executors.newFixedThreadPool(DEFAULT_POOL_SIZE);
118+
return this.service!=null? this.service : Executors.newSingleThreadExecutor();
119119
}
120120

121121
/**
@@ -128,15 +128,16 @@ private ExecutorService getService() {
128128
public final String update(final String sessionId) throws ApplicationException, IOException {
129129
Builder message;
130130
Queue<Builder> messages = this.list.get(sessionId);
131-
while((message = messages.poll()) == null) {
132-
try {
133-
Thread.sleep(TIMEOUT);
134-
} catch (InterruptedException e) {
135-
throw new ApplicationException(e.getMessage(), e);
131+
synchronized(monitor) {
132+
while((message = messages.poll()) == null) {
133+
try {
134+
monitor.wait(TIMEOUT);
135+
} catch (InterruptedException e) {
136+
throw new ApplicationException(e.getMessage(), e);
137+
}
136138
}
139+
return message.toString();
137140
}
138-
139-
return message.toString();
140141
}
141142

142143
/**
@@ -154,17 +155,21 @@ protected String filter(String text) {
154155
* @param builder
155156
*/
156157
private final void copy(Object meetingCode, Builder builder) {
157-
final Collection<Entry<String, Queue<Builder>>> set = this.list.entrySet();
158-
final Iterator<Entry<String, Queue<Builder>>> iterator = set.iterator();
159-
final List<String> _sessions;
160-
if((_sessions = this.sessions.get(meetingCode)) != null) {
161-
while(iterator.hasNext()) {
162-
Entry<String, Queue<Builder>> list = iterator.next();
163-
if(_sessions.contains(list.getKey())) {
164-
list.getValue().add(builder);
158+
final Collection<Entry<String, Queue<Builder>>> set = this.list.entrySet();
159+
final Iterator<Entry<String, Queue<Builder>>> iterator = set.iterator();
160+
final List<String> _sessions;
161+
162+
if((_sessions = this.sessions.get(meetingCode)) != null) {
163+
while(iterator.hasNext()) {
164+
Entry<String, Queue<Builder>> list = iterator.next();
165+
if(_sessions.contains(list.getKey())) {
166+
synchronized(monitor) {
167+
list.getValue().add(builder);
168+
monitor.notifyAll();
165169
}
166170
}
167171
}
172+
}
168173
}
169174

170175
@Override

0 commit comments

Comments
 (0)