Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes Reduce CPU usage for a thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Reduce CPU usage for a thread" Watch "Reduce CPU usage for a thread" New topic
Author

Reduce CPU usage for a thread

Aayush Singhal
Ranch Hand

Joined: Jan 06, 2011
Posts: 47
I use a thread that does nothing much except call "repaint()" method.
it's something like this :
while(true)
{
Thread.sleep(10);
x++;
repaint();
}

Its CPU usage is very high, about 25%. I need to reduce that as it is causing trouble in other applications running on the machine.

How do i do that?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19755
    
  20

Welcome to the Ranch!

First of all, read this. Your repaint() should be done on the EDT. Switch to a javax.swing.Timer instead. You won't need to call Thread.sleep yourself; just schedule the Timer to run at the desired interval.

Secondly, Thread.sleep(10) sleeps 10 milliseconds. In other words, hardly at all. You could just as well remove the call. If you meant to sleep 10 seconds you must use 10000.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Aayush Singhal
Ranch Hand

Joined: Jan 06, 2011
Posts: 47
Thanks. But the reason i use 10 milliseconds sleep is that I need to move a label from right to left on the screen. so after each sleep, i change the x-coordinate of the label by 5. so it looks as if it is moving.
is there any other way to do it i.e. to make a label move across the screen?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19755
    
  20

Doesn't even show up with any CPU usage on my system.
Aayush Singhal
Ranch Hand

Joined: Jan 06, 2011
Posts: 47
Thanks Rob for such efforts.
Your code run fine on an independent application.
But since I have to integrate this scroll into an existing application, I need to call repaint() method everytime to update the position. It is not doing it on its own. That is why CPU usage goes up I guess.
I tried using the javax.Swing.Timer class and it reduced the CPU Usage in the normal mode to a great extent. But when the application runs in High Resolution mode (My application has these 2 modes), it is still crashing the application. Is there a way to get past that?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19755
    
  20

Why do you need to repaint most of your user interface if only one label is moving? How did you implement that label, as a JLabel or as a custom-drawn String? In the first case, calling setLocation should be enough. In the second case you don't need to repaint the entire screen - just the parts that are changed. Instead of repaint(), call repaint(x, y, w, h) where the rectangle of x, y, w and h contains both the old and new location of the label. Or call repaint(x, y, w, h) twice, once for the old location and once for the new one.
Aayush Singhal
Ranch Hand

Joined: Jan 06, 2011
Posts: 47
Rob's a genius!!!
Thanks a ton!!!
Please close the thread.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19755
    
  20

You're welcome
However, we don't close threads, in case someone later has the same problem. He/she can then continue in this thread. Or someone may find an even better solution.
Aayush Singhal
Ranch Hand

Joined: Jan 06, 2011
Posts: 47
Okay.
Will come back to the forum again with some other problem.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39773
    
  28
Aayush Singhal wrote:Okay.
Will come back to the forum again with some other problem.
Is that a promise or a threat?


Look forwards to seeing you back
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reduce CPU usage for a thread