File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Need to reduce the CPU usage when thread is running. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Need to reduce the CPU usage when thread is running." Watch "Need to reduce the CPU usage when thread is running." New topic
Author

Need to reduce the CPU usage when thread is running.

Balaji Soundarajan
Ranch Hand

Joined: Jan 23, 2009
Posts: 59
I am using thread concept for my application development, when i am trying to execute the thread it hast to listen form 120 seconds for check whether any command is came for process or not, during the 120 seconds the my CPU usage was very high, i have to reduce the utilization of the CPU usage.

Please do the needful

Regards
-Balaji.S
Sanju Thomas
Ranch Hand

Joined: Dec 29, 2004
Posts: 243
what kind of job your thread does ? its doing some kind of IO operation ? then the CPU usage might go up. Not sure if you can reduce it using your code.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Probably you're doing something like



Don't do that -- it does, indeed, use lots of CPU time. Instead use wait() and notify(). There's a form of wait() that takes a wait time as an argument, which could be helpful.


[Jess in Action][AskingGoodQuestions]
Balaji Soundarajan
Ranch Hand

Joined: Jan 23, 2009
Posts: 59
Thanks for your reply.

Here i don't want to make the thread to wait status, it has to monitor the command from the client simultaneously for 120 seconds with out using CPU usage.

Thanks
--Balaji.S
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Balaji Soundarajan wrote:
Here i don't want to make the thread to wait status, it has to monitor the command from the client simultaneously for 120 seconds with out using CPU usage.


That's exactly what wait/notify is for. The command thread calls wait() on some object. Then when a message arrives from the client, the client thread calls notify() on that same object. The command thread gets the notification and wakes up, processes the command, and calls wait() again to wait for another command.
Sanju Thomas
Ranch Hand

Joined: Dec 29, 2004
Posts: 243
I agree. You got to wait until the command comes from the client. once the command reaches, you can notify your waiting thread so that it can process the command.
 
Consider Paul's rocket mass heater.
 
subject: Need to reduce the CPU usage when thread is running.