Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed concurrency issue on EventThread class #108

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 2 additions & 23 deletions src/main/java/io/socket/thread/EventThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ public Thread newThread(Runnable runnable) {

private static EventThread thread;

private static ExecutorService service;

private static int counter = 0;

private static final ExecutorService service = Executors.newSingleThreadExecutor(THREAD_FACTORY);

private EventThread(Runnable runnable) {
super(runnable);
Expand Down Expand Up @@ -64,32 +61,14 @@ public static void exec(Runnable task) {
* @param task
*/
public static void nextTick(final Runnable task) {
ExecutorService executor;
synchronized (EventThread.class) {
counter++;
if (service == null) {
service = Executors.newSingleThreadExecutor(THREAD_FACTORY);
}
executor = service;
}

executor.execute(new Runnable() {
service.execute(new Runnable() {
@Override
public void run() {
try {
task.run();
} catch (Throwable t) {
logger.log(Level.SEVERE, "Task threw exception", t);
throw t;
} finally {
synchronized (EventThread.class) {
counter--;
if (counter == 0) {
service.shutdown();
service = null;
thread = null;
}
}
}
}
});
Expand Down