guava icon indicating copy to clipboard operation
guava copied to clipboard

TimeLimiter.callWithTimeout Not working

Open Gowtham-Gowts opened this issue 7 years ago • 2 comments

While using TimeLimiter.callWithTimeout(Callable callable, Duration timeout). After the given timeout the thread is not interrupted. Please Help on this. Sample Implementation:

TimeLimiter stl =  SimpleTimeLimiter.create(callTimeoutPool);
            stl.callWithTimeout(new Callable<Object>() {
                @Override
                public Object call()
                {
//Logic here
                }
            }, callTimeout, TimeUnit.MILLISECONDS);
        }

Gowtham-Gowts avatar Aug 05 '19 04:08 Gowtham-Gowts

I would recommend asking for help on Stack Overflow and providing a more complete example. If it does emerge that there's a plausible defect in the library please reopen with the specific code that fails. Thanks!

kevinb9n avatar Aug 05 '19 20:08 kevinb9n

Sample code

               Long callTimeout = 1L;

                ExecutorService callTimeoutPool= Executors.newSingleThreadExecutor( new ThreadFactoryBuilder().setNameFormat("name").setDaemon(true).build());
                LOGGER.log(Level.INFO, "[ TimeoutImpl : timedCall ] currentThread name = {0}", new Object[]{Thread.currentThread().getName()});

                try
                {
                        new SimpleTimeLimiter().callWithTimeout(new Callable(){
                        @Override
                        public Object call()
                        {
                                int i=0;
                                while(true)
                                {
                                        System.out.println(i++);
                                        if(false)
                                                break;
                                }
                                return true;
                        }
                        }, callTimeout, TimeUnit.MILLISECONDS, true);
                }
                catch(InterruptedException e)
                {
                        LOGGER.log(Level.SEVERE, "[ TimeoutImpl : timedCall ]  InterruptedException" ,e );
                        Thread.currentThread().interrupt();
                        callTimeoutPool.shutdownNow();
                }
                catch(Exception e)
                {
                        LOGGER.log(Level.SEVERE, "[ TimeoutImpl : timedCall ]  Exception" ,e );
                        Thread.currentThread().interrupt();
                        callTimeoutPool.shutdownNow();
                }
        }

please correct me what i did wrong on this implementation @kevinb9n

Gowtham-Gowts avatar Aug 06 '19 05:08 Gowtham-Gowts