Skip to content

Commit 022d330

Browse files
committed
Use Lock to instead of synchronized block.
1 parent 0091409 commit 022d330

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

src/tinystruct/examples/talk.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@
1717
import java.util.concurrent.ExecutorService;
1818
import java.util.concurrent.Executors;
1919
import java.util.concurrent.TimeUnit;
20+
import java.util.concurrent.locks.Condition;
21+
import java.util.concurrent.locks.Lock;
22+
import java.util.concurrent.locks.ReentrantLock;
2023

2124
import org.tinystruct.AbstractApplication;
2225
import org.tinystruct.ApplicationException;
26+
import org.tinystruct.ApplicationRuntimeException;
2327
import org.tinystruct.data.component.Builder;
2428
import org.tinystruct.system.ApplicationManager;
2529

@@ -31,14 +35,17 @@ public class talk extends AbstractApplication {
3135
protected final Map<String, Queue<Builder>> list = new ConcurrentHashMap<String, Queue<Builder>>();
3236
protected final Map<String, List<String>> sessions = new ConcurrentHashMap<String, List<String>>();
3337
private ExecutorService service;
38+
private Lock lock = new ReentrantLock();
39+
private Condition consumer = lock.newCondition();
40+
private Condition producer = lock.newCondition();
3441

3542
@Override
3643
public void init() {
3744
this.setAction("talk/update", "update");
3845
this.setAction("talk/save", "save");
3946
this.setAction("talk/version", "version");
4047
this.setAction("talk/testing", "testing");
41-
48+
4249
if (this.service != null) {
4350
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
4451
@Override
@@ -131,17 +138,17 @@ public final String update(final String sessionId) throws ApplicationException,
131138
Queue<Builder> messages = this.list.get(sessionId);
132139
// If there is a new message, then return it directly
133140
if((message = messages.poll()) != null) return message.toString();
134-
135-
synchronized(talk.class) {
136-
while((message = messages.poll()) == null) {
137-
try {
138-
talk.class.wait(TIMEOUT);
139-
} catch (InterruptedException e) {
140-
throw new ApplicationException(e.getMessage(), e);
141-
}
141+
lock.lock();
142+
while((message = messages.poll()) == null) {
143+
try {
144+
consumer.await(TIMEOUT, TimeUnit.MICROSECONDS);
145+
} catch (InterruptedException e) {
146+
throw new ApplicationException(e.getMessage(), e);
142147
}
143-
return message.toString();
144148
}
149+
producer.signalAll();
150+
lock.unlock();
151+
return message.toString();
145152
}
146153

147154
/**
@@ -168,10 +175,15 @@ private final void copy(Object meetingCode, Builder builder) {
168175
while(iterator.hasNext()) {
169176
Entry<String, Queue<Builder>> list = iterator.next();
170177
if(_sessions.contains(list.getKey())) {
171-
synchronized(talk.class) {
172-
list.getValue().add(builder);
173-
talk.class.notifyAll();
178+
lock.lock();
179+
try {
180+
producer.await();
181+
} catch (InterruptedException e) {
182+
throw new ApplicationRuntimeException(e.getMessage(), e);
174183
}
184+
list.getValue().add(builder);
185+
consumer.signalAll();
186+
lock.unlock();
175187
}
176188
}
177189
}

0 commit comments

Comments
 (0)