It is required to periodically and independently from each other change the parameters of the view-elements (the text on the buttons). For this purpose, used Timer and TimerTask . Everything is working. But I created a lot of Timer and TimerTask : for each button (10 pieces). Timers are started for each button simultaneously. As I understand it, 10 separate threads are created and this is not good? Is there any need to optimize the program architecture in this case, for example, to use ExecutorService to create a pool or something else? Or is my implementation optimal?

And another question: how to use ExecutorService with Timer and TimerTask ? Nowhere found examples of their sharing.

    3 answers 3

    Using ExecutorService can help you only if you sometimes stop (kill) the stream, and then start some. If this is the case, then inactive threads will be reused when trying to start a new TimerTask in the ExecutorService .

    In the event that you have a number of running TimerTask statically (say, on the form of n buttons, TimerTask hangs on each, the number of buttons does not change during the program), then using ExecutorService will not help you.

    And don't forget to call shutdown() or shutdownNow() when you finish working with ExecutorService .

    • Yes. I periodically stop some flow, then once I resume it. Then the next question: how to use ExecutorService with Timer and TimerTask ? Nowhere found examples of their sharing. - Doraemon
    • one
      @Doraemon You know, after your question I became interested in myself, is it possible to come up with something in your situation with ExecutorService and came to the conclusion that using Timer is the most optimal. ExecutorService does not fit in your context, since the Timer class is simply thrown away, and without it all the benefits of using TimerTask are reduced to zero. Use Timer and do not complicate your life with micro-optimizations). If I helped you - you can mark the issue resolved. Otherwise - ask questions, I will answer with pleasure) - PloadyFree

    For this task, if you have tested and are satisfied with everything, I see no reason to redo it. ExecutorService in this case will not give the best results. Yes, you create 10 threads, but they all sleep until the timer runs, so this is not a problem.

      I solved similar problems a little bit differently. For everything about everything you need one timer, which will work every second or (every tenth of a second, as appropriate). Also need a hit counter. Now, with each timer triggered, we increase the counter by one. The next piece of code will be one switch (or a group of if, as appropriate) where it will decide what to do.

      As a result, one timer is easier to manage. But for a dozen timers, you usually need to keep an array. Also, in the case of multiple timers, debugging can turn into a nightmare. And yet, in some cases, it is impossible to guarantee the order of operation of the timers. And it can greatly ruin the logic.

      • Yes, a good decision, it also occurs in my projects, but it is not always possible to introduce this practice well, sometimes it is simply technically impossible. - PloadyFree