1212import java .util .Queue ;
1313import java .util .concurrent .ConcurrentHashMap ;
1414import java .util .concurrent .ConcurrentLinkedQueue ;
15+ import java .util .concurrent .ExecutorService ;
16+ import java .util .concurrent .Executors ;
17+ import java .util .concurrent .TimeUnit ;
1518
1619import org .tinystruct .AbstractApplication ;
1720import org .tinystruct .ApplicationException ;
@@ -24,13 +27,32 @@ public class talk extends AbstractApplication {
2427 protected final Map <String , Queue <Builder >> list = new ConcurrentHashMap <String , Queue <Builder >>();
2528 protected final Map <String , Queue <Builder >> meetings = new ConcurrentHashMap <String , Queue <Builder >>();
2629 protected final Map <String , List <String >> sessions = new ConcurrentHashMap <String , List <String >>();
30+ private final ExecutorService service = Executors .newFixedThreadPool (3 );
2731
2832 @ Override
2933 public void init () {
3034 this .setAction ("talk/update" , "update" );
3135 this .setAction ("talk/save" , "save" );
3236 this .setAction ("talk/version" , "version" );
3337 this .setAction ("talk/testing" , "testing" );
38+
39+ Runtime .getRuntime ().addShutdownHook (new Thread (new Runnable () {
40+ @ Override
41+ public void run () {
42+ service .shutdown ();
43+ while (true ) {
44+ try {
45+ System .out .println ("Waiting for the service to terminate..." );
46+ if (service .awaitTermination (5 , TimeUnit .SECONDS )) {
47+ System .out .println ("Service will be terminated soon." );
48+ break ;
49+ }
50+ } catch (InterruptedException e ) {
51+ e .printStackTrace ();
52+ }
53+ }
54+ }
55+ }));
3456 }
3557
3658 /**
@@ -75,7 +97,7 @@ public final String save(final Object meetingCode, final Builder builder) {
7597 this .meetings .notifyAll ();
7698 }
7799
78- new Thread (new Runnable (){
100+ service . execute (new Runnable (){
79101 @ Override
80102 public void run () {
81103 synchronized (talk .this .meetings ) {
@@ -91,7 +113,7 @@ public void run() {
91113 talk .this .copy (meetingCode , message );
92114 }
93115 }
94- }). start () ;
116+ });
95117 return builder .toString ();
96118 }
97119
@@ -172,7 +194,7 @@ public boolean testing(final int n) throws ApplicationException {
172194 sess .add ("{B}" );
173195 this .sessions .put ("[M001]" , sess );
174196
175- new Thread (new Runnable (){
197+ service . execute (new Runnable (){
176198 @ Override
177199 public void run () {
178200 int i =0 ;
@@ -188,9 +210,9 @@ public void run() {
188210 e .printStackTrace ();
189211 }
190212 }
191- }). start () ;
213+ });
192214
193- new Thread (new Runnable (){
215+ service . execute (new Runnable (){
194216 @ Override
195217 public void run () {
196218 int i =0 ;
@@ -206,9 +228,9 @@ public void run() {
206228 e .printStackTrace ();
207229 }
208230 }
209- }). start () ;
231+ });
210232
211- new Thread (new Runnable (){
233+ service . execute (new Runnable (){
212234 @ Override
213235 public void run () {
214236 // TODO Auto-generated method stub
@@ -225,9 +247,9 @@ public void run() {
225247 e .printStackTrace ();
226248 }
227249 }
228- }). start () ;
250+ });
229251
230- new Thread (new Runnable (){
252+ service . execute (new Runnable (){
231253 @ Override
232254 public void run () {
233255 // TODO Auto-generated method stub
@@ -244,7 +266,7 @@ public void run() {
244266 e .printStackTrace ();
245267 }
246268 }
247- }). start () ;
269+ });
248270
249271 return true ;
250272 }
0 commit comments