简体   繁体   中英

How to add a timer object to perform every X seconds actions on one thread and every Y sec on the other one?

What is the best approach to make a few threads which perform some action asynchronous on JPanel canvas, and can be controlled form main(...)? What about synchronization techniques?

java.util.Timer works with tasks. Each task has its own scheduling. You can create task per thread and schedule them appropriately. Each task should be able to send signal to its thread (eg using notify() ).

So, you will achieve this functionality: each task notifies its thread and is scheduled separately. And this system is scalable: timer creates only one own thread, so even if number of your working threads will grow you will have only one additional thread in your system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM