Skip to content

Commit 0f99b47

Browse files
authored
Merge pull request #181 from BobHanson/hanson1
Executors.submit(Runnable) not returning a Future
2 parents f414fb0 + 98aa96f commit 0f99b47

File tree

3 files changed

+49
-30
lines changed

3 files changed

+49
-30
lines changed

sources/net.sf.j2s.java.core/src/java/util/concurrent/Executors.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -635,12 +635,6 @@ public boolean awaitTermination(long timeout, TimeUnit unit)
635635
return e.awaitTermination(timeout, unit);
636636
}
637637
public Future<?> submit(Runnable task) {
638-
/**
639-
* @j2sNative
640-
*
641-
* task.run();
642-
* return true;
643-
*/
644638
return e.submit(task);
645639
}
646640
public <T> Future<T> submit(Callable<T> task) {

sources/net.sf.j2s.java.core/src/java/util/concurrent/ScheduledThreadPoolExecutor.java

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -283,30 +283,38 @@ boolean canRunInCurrentRunState(boolean periodic) {
283283
executeExistingDelayedTasksAfterShutdown);
284284
}
285285

286-
/**
287-
* Main execution method for delayed or periodic tasks. If pool
288-
* is shut down, rejects the task. Otherwise adds task to queue
289-
* and starts a thread, if necessary, to run it. (We cannot
290-
* prestart the thread to run the task because the task (probably)
291-
* shouldn't be run yet,) If the pool is shut down while the task
292-
* is being added, cancel and remove it if required by state and
293-
* run-after-shutdown parameters.
294-
*
295-
* @param task the task
296-
*/
297-
private void delayedExecute(RunnableScheduledFuture<?> task) {
298-
if (isShutdown())
299-
reject(task);
300-
else {
301-
super.getQueue().add(task);
302-
if (isShutdown() &&
303-
!canRunInCurrentRunState(task.isPeriodic()) &&
304-
remove(task))
305-
task.cancel(false);
306-
else
307-
prestartCoreThread();
308-
}
309-
}
286+
/**
287+
* Main execution method for delayed or periodic tasks. If pool is shut down,
288+
* rejects the task. Otherwise adds task to queue and starts a thread, if
289+
* necessary, to run it. (We cannot prestart the thread to run the task because
290+
* the task (probably) shouldn't be run yet,) If the pool is shut down while the
291+
* task is being added, cancel and remove it if required by state and
292+
* run-after-shutdown parameters.
293+
*
294+
* @param task the task
295+
*/
296+
@SuppressWarnings("unused")
297+
private void delayedExecute(RunnableScheduledFuture<?> task) {
298+
if (isShutdown())
299+
reject(task);
300+
else {
301+
if (task.isPeriodic()) {
302+
swingjs.JSUtil.notImplemented("Periodic tasks");
303+
}
304+
305+
if (/** @j2sNative true || */
306+
false) {
307+
new Thread(task).start();
308+
} else /** @j2sIgnore */
309+
{
310+
super.getQueue().add(task);
311+
if (isShutdown() && !canRunInCurrentRunState(task.isPeriodic()) && remove(task))
312+
task.cancel(false);
313+
else
314+
prestartCoreThread();
315+
}
316+
}
317+
}
310318

311319
/**
312320
* Requeues a periodic task unless current run state precludes it.

sources/net.sf.j2s.java.core/src/test/Test_Future.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import java.util.concurrent.CompletableFuture;
2323
import java.util.concurrent.CompletionStage;
2424
import java.util.concurrent.Executor;
25+
import java.util.concurrent.ExecutorService;
26+
import java.util.concurrent.Executors;
27+
import java.util.concurrent.Future;
2528
import java.util.function.Supplier;
2629

2730
import javax.swing.JButton;
@@ -50,8 +53,22 @@ private void btnAction() {
5053
CompletionStage<String> future = longJob();
5154
future.thenAccept((value) -> {
5255
System.out.format("returned with %s%n", value);
56+
});
57+
ExecutorService dialogExecutor = Executors.newSingleThreadExecutor();
58+
Future<?> f = dialogExecutor.submit(() -> {
59+
System.out.println("dialog runnable 1");
60+
});
61+
dialogExecutor.submit(() -> {
62+
System.out.println("dialog runnable 2");
63+
});
64+
dialogExecutor.submit(() -> {
65+
System.out.println("dialog runnable 3");
66+
System.out.println(future.toString());
5367
});
5468
System.out.println("CompletionStage started");
69+
System.out.println(future.toString());
70+
71+
5572
}
5673

5774
CompletableFuture<String> longJob() {

0 commit comments

Comments
 (0)