Can I saturate a program with Timer objects?

I have a program for which I plan to use many Timer objects, and, as I understand it, each timer runs its own thread on it. So I wondered if it was possible to start so many timers that it was detrimental to the performance of a program with too many threads.

For example, I thought that several (logical, timer) pairs have logical values ​​that are inverted at several different time intervals.

+3
source share
2 answers

Yes, Timerobjects consume thread resources, so you can set a limit in the JVM. If your goal is to schedule tasks at different times, you can look at one of the many Java ExecutorService implementations , such as ScheduledThreadPoolExecutor . The Executors class provides a convenient factory for creating these objects. Several implementations use thread pools, which you can configure to determine how many tasks can be performed simultaneously. You can also use output that creates tasks (if any), and turn off tasks in an orderly way if your program needs to exit.

+3
source

. , , . , @Rob, ScheduledThreadPoolExecutor.

0

Source: https://habr.com/ru/post/1795647/


All Articles