2

I defined a Future in the following way:

        Future<?>       future  = null;
        ExecutorService service = null;

First I used (just playing to get to know the stuff):

            future = service.submit(() -> {
                    for (int i = 0; i < 5; ++i) {
                        System.out.println("Printing record old: " + i);
                        try {
                            Thread.sleep(5);
                        } catch (InterruptedException e) {
                            // Ignore
                        }
                    }
                });

But I really did not like the try catch part, so I rewrote it to:

            future = service.submit(() -> {
                    for (int i = 0; i < 5; ++i) {
                        System.out.println("Printing record: " + i);
                        Thread.sleep(5);
                    }
                    return "Done";
                });

In this way a Callable is used instead of a Runnable and I do not need the catch. But I return an unused value.
Is it OK to do it in this way, or is there a better way?

Cecil
  • 41
  • 7
  • 4
    If I were you, I'd stay away from `Thread.sleep` in the first place. If you need something to be done every N seconds, use a `Timer` or a `ScheduledExecutorService`. – RealSkeptic Jul 15 '19 at 13:09
  • @RealSkeptic I only added the sleep to make the runtime of the thread longer. ;-) – Cecil Jul 15 '19 at 13:27
  • @NathanHughes But I do not really return a value. I only do that so it becomes a Callable instead of a Runnable and I do not have to use the try catch I need to use when it is a Runnable. – Cecil Jul 15 '19 at 14:13

2 Answers2

1

It is because of method syntax

Runnable Runnable class run method does not throw any exception so you need to handle any checked exception using try catch

void run()

Callable But Callable class call method throws Exception, so either you can handle using try catch or leave for JVM

V call() throws Exception
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • That is what I mend: I make sure that it is a Callable so I do not have to use a try catch. – Cecil Jul 15 '19 at 13:30
  • 1
    but i won't recommend to use `Callable` until you have requirement to return something @Cecil one line of code in try catch is not going to end world i believe – Ryuzaki L Jul 15 '19 at 13:32
  • It is not one line, but three. And personally I find the code more beautiful. But if I was sure it was correct, I would not have asked. :-D – Cecil Jul 15 '19 at 13:38
0

When using Callable, if an exception is thrown you will get ExecutionException while calling future.get().

This exception will wrap the exception thrown by your callable, and can be accessed by getCause()

See more here: https://www.baeldung.com/java-runnable-callable

So you're not really avoiding any try/catch, just move it somewhere else (which makes sense- you need to handle it somewhere...)

Nir Levy
  • 12,750
  • 3
  • 21
  • 38
  • At the moment I just do not want to use try catch, but your information is certainly interesting. – Cecil Jul 15 '19 at 14:02