I need to run a particular thread at a certain interval of time. I saw in this SO link that using timer is same as while(true) thread.sleep if we use it for delaying a certain period of time. What are the difference between these 3 and which of the above 3 stated methods will be effective and will be useful in reducing CPU usage.
Asked
Active
Viewed 1,305 times
2
-
Uh, there are fundamental differences between everything you mention since some of these affect the current threads while others will affect different threads; so, what it is you want to do? And no, sleep is not a busy loop and will _not_ eat CPU time anyway – fge Apr 16 '14 at 10:31
-
@fge If I use while(true){thread.sleep()} separately, it doesn't take 100% CPU usage. But if I have 4 or 5 loops like that in my code, CPU usage goes to 100% – Joker Apr 16 '14 at 11:09
-
Then you're doing something really wrong somewhere. I have never observed that behaviour myself. – fge Apr 16 '14 at 11:18
-
@fge I didn't write the full code. The project needs all the activities to be run in parallel and the coder has extended every class as thread. Does that increase CPU usage? I think nearly 10 - 20 threads are there. – Joker Apr 17 '14 at 04:12