Skip to content
Advertisement

Using Java stream forEach() in ScheduledExecutorService freezes

The general idea is to have a Runnable running every 10 seconds in background to check some data and if needed make changes in an object. ScheduledExecutorService is instantiated in method main() and the task is scheduled. Runnable task instantiates Crawler object and starts crawling. Most of the times it runs couple of times with success but when application is running and data changes one of crawler’s method is fired but never ends. There is no loop in the code. I was trying to debug also without success. Maybe you will be able to spot where the problem lays.

Main:

JavaScript

EveryTenSeconds Runnable:

JavaScript

Crawler:

JavaScript

BookEntry

JavaScript

Advertisement

Answer

It seems that the answer is easy – whenever the task scheduled in ScheduledExecutorService throws an exception the task is halted and never repeated. Also the exception is not thrown visibly. The easiest way to avoid such situation is to have try-catch block in run() ,method of Runnable. Please have a look at this post: ScheduledExecutorService handling exceptions

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement