[concurrency-interest] Simple ScheduledFuture problem

Tim Peierls tim at peierls.net
Tue Aug 22 09:47:17 EDT 2006


Also, instead of catching Exception, you can catch the more specific
ExecutionException -- thrown by Future.get() -- and examine the underlying
Throwable with getCause().

--tim

On 8/22/06, robert lazarski <robertlazarski at gmail.com> wrote:
> >
> > I finally got some time to implement the latest suggestions of David
> > and Tim. This is what I came up with:
> >
> > class PingTask implements Runnable {
> >
> >          public void run() {
> >
> >              Future f = null;
> >              boolean interrupted = false;
> >              try {
> >                  ExecutorService exec = Executors.newCachedThreadPool();
> >                  f = exec.submit(new PingFuture());
> >                  f.get(5000, TimeUnit.MILLISECONDS);
> >
> >              } catch (TimeoutException ex) {
> >                    interrupted = true;
> >                    logger.error("Future timed out: \n" + ex.getMessage(),
> > ex);
> >              } catch (InterruptedException ex) {
> >                    interrupted = true;
> >                    logger.error ("Future interrupted: \n" +
> > ex.getMessage(), ex);
> >              } catch (Exception ex) {
> >                    logger.error(ex.getMessage(), ex);
> >              }
> >              finally {
> >                  f.cancel(true);
> >                  logger.debug("CURRENT STATE: " + getMessage(flag));
> >
> >              }
> >              if (interrupted) {
> >                  Thread.currentThread().interrupt();
> >              }
> >          }
> >      } // end inner class PingTask
> >
> > Thanks for the feedback!
> > Robert
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20060822/f6c70faa/attachment.html 


More information about the Concurrency-interest mailing list