From b265950e810e61a52ae2002c660e695c7464ea1c Mon Sep 17 00:00:00 2001 From: "James M.Z" Date: Wed, 11 Jan 2017 13:11:13 +0800 Subject: [PATCH] Use ThreadPool to handle threads, it will improve the performance and avoid the message missing issue. --- src/tinystruct/examples/talk.java | 42 +++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/src/tinystruct/examples/talk.java b/src/tinystruct/examples/talk.java index 75f7bcd..94dd4bd 100644 --- a/src/tinystruct/examples/talk.java +++ b/src/tinystruct/examples/talk.java @@ -12,6 +12,9 @@ import java.util.Queue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; import org.tinystruct.AbstractApplication; import org.tinystruct.ApplicationException; @@ -24,6 +27,7 @@ public class talk extends AbstractApplication { protected final Map> list = new ConcurrentHashMap>(); protected final Map> meetings = new ConcurrentHashMap>(); protected final Map> sessions = new ConcurrentHashMap>(); + private final ExecutorService service = Executors.newFixedThreadPool(3); @Override public void init() { @@ -31,6 +35,24 @@ public void init() { this.setAction("talk/save", "save"); this.setAction("talk/version", "version"); this.setAction("talk/testing", "testing"); + + Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { + @Override + public void run() { + service.shutdown(); + while (true) { + try { + System.out.println("Waiting for the service to terminate..."); + if (service.awaitTermination(5, TimeUnit.SECONDS)) { + System.out.println("Service will be terminated soon."); + break; + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + })); } /** @@ -75,7 +97,7 @@ public final String save(final Object meetingCode, final Builder builder) { this.meetings.notifyAll(); } - new Thread(new Runnable(){ + service.execute(new Runnable(){ @Override public void run() { synchronized(talk.this.meetings) { @@ -91,7 +113,7 @@ public void run() { talk.this.copy(meetingCode, message); } } - }).start(); + }); return builder.toString(); } @@ -172,7 +194,7 @@ public boolean testing(final int n) throws ApplicationException { sess.add("{B}"); this.sessions.put("[M001]", sess); - new Thread(new Runnable(){ + service.execute(new Runnable(){ @Override public void run() { int i=0; @@ -188,9 +210,9 @@ public void run() { e.printStackTrace(); } } - }).start(); + }); - new Thread(new Runnable(){ + service.execute(new Runnable(){ @Override public void run() { int i=0; @@ -206,9 +228,9 @@ public void run() { e.printStackTrace(); } } - }).start(); + }); - new Thread(new Runnable(){ + service.execute(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub @@ -225,9 +247,9 @@ public void run() { e.printStackTrace(); } } - }).start(); + }); - new Thread(new Runnable(){ + service.execute(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub @@ -244,7 +266,7 @@ public void run() { e.printStackTrace(); } } - }).start(); + }); return true; }