Skip to content

Commit f479a26

Browse files
committed
Update talk.java to improve the performance for message update.
1 parent 40b1f8b commit f479a26

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/tinystruct/examples/talk.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public class talk extends AbstractApplication {
3131
protected final Map<String, Queue<Builder>> list = new ConcurrentHashMap<String, Queue<Builder>>();
3232
protected final Map<String, List<String>> sessions = new ConcurrentHashMap<String, List<String>>();
3333
private ExecutorService service;
34-
private final Object monitor = new Object();
3534

3635
@Override
3736
public void init() {
@@ -130,10 +129,13 @@ private ExecutorService getService() {
130129
public final String update(final String sessionId) throws ApplicationException, IOException {
131130
Builder message;
132131
Queue<Builder> messages = this.list.get(sessionId);
133-
synchronized(monitor) {
132+
// If there is a new message, then return it directly
133+
if((message = messages.poll()) != null) return message.toString();
134+
135+
synchronized(talk.class) {
134136
while((message = messages.poll()) == null) {
135137
try {
136-
monitor.wait(TIMEOUT);
138+
talk.class.wait(TIMEOUT);
137139
} catch (InterruptedException e) {
138140
throw new ApplicationException(e.getMessage(), e);
139141
}
@@ -165,9 +167,9 @@ private final void copy(Object meetingCode, Builder builder) {
165167
while(iterator.hasNext()) {
166168
Entry<String, Queue<Builder>> list = iterator.next();
167169
if(_sessions.contains(list.getKey())) {
168-
synchronized(monitor) {
170+
synchronized(talk.class) {
169171
list.getValue().add(builder);
170-
monitor.notifyAll();
172+
talk.class.notifyAll();
171173
}
172174
}
173175
}
@@ -200,6 +202,7 @@ public boolean testing(final int n) throws ApplicationException {
200202
this.getService().execute(new Runnable(){
201203
@Override
202204
public void run() {
205+
System.out.println(Thread.currentThread().getId());
203206
int i=0;
204207
while(i++<n)
205208
try {
@@ -218,6 +221,7 @@ public void run() {
218221
this.getService().execute(new Runnable(){
219222
@Override
220223
public void run() {
224+
System.out.println(Thread.currentThread().getId());
221225
int i=0;
222226
while(i++<n)
223227
try {
@@ -236,6 +240,7 @@ public void run() {
236240
this.getService().execute(new Runnable(){
237241
@Override
238242
public void run() {
243+
System.out.println(Thread.currentThread().getId());
239244
// TODO Auto-generated method stub
240245
System.out.println("[A] is started...");
241246
while(true)
@@ -255,6 +260,7 @@ public void run() {
255260
this.getService().execute(new Runnable(){
256261
@Override
257262
public void run() {
263+
System.out.println(Thread.currentThread().getId());
258264
// TODO Auto-generated method stub
259265
System.out.println("[B] is started...");
260266
while(true)

0 commit comments

Comments
 (0)