Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions src/tinystruct/examples/talk.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,13 +27,32 @@ public class talk extends AbstractApplication {
protected final Map<String, Queue<Builder>> list = new ConcurrentHashMap<String, Queue<Builder>>();
protected final Map<String, Queue<Builder>> meetings = new ConcurrentHashMap<String, Queue<Builder>>();
protected final Map<String, List<String>> sessions = new ConcurrentHashMap<String, List<String>>();
private final ExecutorService service = Executors.newFixedThreadPool(3);

@Override
public void init() {
this.setAction("talk/update", "update");
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();
}
}
}
}));
}

/**
Expand Down Expand Up @@ -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) {
Expand All @@ -91,7 +113,7 @@ public void run() {
talk.this.copy(meetingCode, message);
}
}
}).start();
});
return builder.toString();
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -244,7 +266,7 @@ public void run() {
e.printStackTrace();
}
}
}).start();
});

return true;
}
Expand Down