Skip to content

Commit feabcfa

Browse files
author
Simone Bordet
committed
Guarding against submission while stopped and cancelling pending tasks on stop.
1 parent dade6d1 commit feabcfa

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

‎cometd-javascript/common-test/src/main/java/org/cometd/javascript/JavaScriptThreadModel.java‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.concurrent.ExecutionException;
2626
import java.util.concurrent.FutureTask;
2727
import java.util.concurrent.LinkedBlockingQueue;
28+
import java.util.concurrent.RejectedExecutionException;
2829
import java.util.concurrent.atomic.AtomicInteger;
2930

3031
import org.mozilla.javascript.Context;
@@ -57,13 +58,16 @@ public JavaScriptThreadModel(ScriptableObject rootScope)
5758
public void init() throws Exception
5859
{
5960
assert rootScope != null;
61+
queue.clear();
6062
running = true;
6163
thread.start();
6264
}
6365

6466
public void destroy() throws Exception
6567
{
6668
running = false;
69+
for (FutureTask<Object> task : queue)
70+
task.cancel(false);
6771
thread.interrupt();
6872
thread.join();
6973
}
@@ -217,9 +221,16 @@ private Object invoke(boolean sync, Callable<Object> invocation)
217221
private void submit(FutureTask<Object> future)
218222
{
219223
if (Thread.currentThread() == thread)
224+
{
220225
future.run();
226+
}
221227
else
222-
queue.offer(future);
228+
{
229+
if (running)
230+
queue.offer(future);
231+
else
232+
throw new RejectedExecutionException();
233+
}
223234
}
224235

225236
public void define(final Class clazz) throws InvocationTargetException, IllegalAccessException, InstantiationException

0 commit comments

Comments
 (0)