Future.get hangs when Callback catches InterruptedException

Eugene To

This code hangs. I understand that I call future.get() for the future that wasn't executed but I expect to see CancellationExcetpion or InterruptedException or some other exception on future.get() method. Am I wrong?

public class ExecutorTest {
    static ExecutorService executorService = Executors.newFixedThreadPool(1);
    public static void main(String[] args) throws Exception {
        List<Future> futures = new ArrayList<Future>();
        for (int i = 0; i < 10; i++) {
            Future<Object> future = executorService.submit(new Callable<Object>() {
                @Override
                public Object call() throws Exception {
                    System.out.println("Start task");
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("Finish task");
                    return "anything";
                }
            });
            futures.add(future);
        }

        new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(10000);
                    System.out.println("Shutdown Now");
                    executorService.shutdownNow();
                } catch (InterruptedException e) {
                    System.out.println("Interrupted Exception during shutdown");
                }
            }
        }.start();

        for(Future f : futures) {
            System.out.println("-------------------------");
            System.out.println("before sleep");
            Thread.sleep(3000);
            System.out.println("after sleep...");
            try {
                f.get();
            } catch (Exception ex) {
                System.out.println("exception during future.get");
            }
            System.out.println("after get!");
        }

    }
}

This is my output

Start task
-------------------------
before sleep
after sleep...
Finish task
after get!
-------------------------
Start task
before sleep
after sleep...
Finish task
Start task
after get!
-------------------------
before sleep
after sleep...
Finish task
Start task
after get!
-------------------------
before sleep
Shutdown Now
Finish task
java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at ru.pp.ExecutorTest$1.call(ExecutorTest.java:28)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
after sleep...
after get!
-------------------------
before sleep
after sleep...

Java 8, Intellij

If I replace

System.out.println("Start task");
try {
    Thread.sleep(3000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

with this

System.out.println("Start task");
Thread.sleep(3000);

then it doesn't hang

I'm frustrated...

Eugene To

When Thread_1 calls FutureTask#get then Thread_1#wait is called. Now to continue Thread_1 it should be notified by somebody. Notification is called ONLY when FutureTask is terminated (on FutureTask#set, FutureTask#setException or FutureTask#cancel). But in my case the FutureTask was never started because of shutdownNow (FutureTask was in state NEW). So it was never terminated and Thread_1#notify was never called. To fix it I got all not started futures from #shutdownNow and didn't call FutureTask#get for them.

PS: Honestly FutureTask uses sun.misc.Unsafe#park #unpark methods instead of wait and notify.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why should I do Future.cancel() if I get InterruptedException when doing Future.get()

From Dev

JavascriptCore hangs when running a JS with callback

From Dev

When does std::future get executed?

From Dev

Set future from callback

From Dev

Mongoose query hangs after callback

From Dev

get_dir_file_info() hangs when run on a large directory

From Dev

Is there any event that catches when the user turns the corner?

From Dev

Get a callback when user dismisses an Intent dialog

From Dev

Android LocationRequest: get a callback when request expires

From Dev

Callback is returning undefined when called in a get request

From Dev

Get a callback when user dismisses an Intent dialog

From Dev

Get a callback when a WKInterfaceSlider lost WKCrownSequencer focus

From Dev

"Cannot find symbol" when using throw InterruptedException

From Dev

Java methods that get to wait somehow and that throw InterruptedException

From Dev

Convert Javascript callback to Dart future

From Dev

Future in callback run in the same thread

From Dev

Java / Scala Future driven by a callback

From Dev

gulp is not calling the callback -- it just hangs at end of task

From Dev

nodejs: force callback execution if method hangs

From Dev

gulp is not calling the callback -- it just hangs at end of task

From Dev

Xcode debugger hangs in callback function from JavaScriptCore

From Dev

MyBatis hangs when inserting

From Dev

WinForm hangs when opened

From Dev

When to use thread.interrupt() and when to throws InterruptedException

From Dev

What should QApplication::notify() return when it catches an exception

From Dev

Show AlertView on VC. When custom class catches the error in closure

From Dev

What should QApplication::notify() return when it catches an exception

From Dev

js: How navigate to new url and get callBack when page is loaded?

From Dev

Cannot get response in JSONP proxy callback when request failed

Related Related

  1. 1

    Why should I do Future.cancel() if I get InterruptedException when doing Future.get()

  2. 2

    JavascriptCore hangs when running a JS with callback

  3. 3

    When does std::future get executed?

  4. 4

    Set future from callback

  5. 5

    Mongoose query hangs after callback

  6. 6

    get_dir_file_info() hangs when run on a large directory

  7. 7

    Is there any event that catches when the user turns the corner?

  8. 8

    Get a callback when user dismisses an Intent dialog

  9. 9

    Android LocationRequest: get a callback when request expires

  10. 10

    Callback is returning undefined when called in a get request

  11. 11

    Get a callback when user dismisses an Intent dialog

  12. 12

    Get a callback when a WKInterfaceSlider lost WKCrownSequencer focus

  13. 13

    "Cannot find symbol" when using throw InterruptedException

  14. 14

    Java methods that get to wait somehow and that throw InterruptedException

  15. 15

    Convert Javascript callback to Dart future

  16. 16

    Future in callback run in the same thread

  17. 17

    Java / Scala Future driven by a callback

  18. 18

    gulp is not calling the callback -- it just hangs at end of task

  19. 19

    nodejs: force callback execution if method hangs

  20. 20

    gulp is not calling the callback -- it just hangs at end of task

  21. 21

    Xcode debugger hangs in callback function from JavaScriptCore

  22. 22

    MyBatis hangs when inserting

  23. 23

    WinForm hangs when opened

  24. 24

    When to use thread.interrupt() and when to throws InterruptedException

  25. 25

    What should QApplication::notify() return when it catches an exception

  26. 26

    Show AlertView on VC. When custom class catches the error in closure

  27. 27

    What should QApplication::notify() return when it catches an exception

  28. 28

    js: How navigate to new url and get callBack when page is loaded?

  29. 29

    Cannot get response in JSONP proxy callback when request failed

HotTag

Archive