aspose file tools*
The moose likes Java in General and the fly likes How can I control a CPU usages? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How can I control a CPU usages?" Watch "How can I control a CPU usages?" New topic
Author

How can I control a CPU usages?

arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Hi,
I have made a simple StandAlone Application which just takes a no in a text field and prints out same in a textarea. if you give a higher number in it say 1000 or perhaps more higher 10,000 and try to have a look at the Task Managaer for CPU Usage it goes upto 100% while the memory usage remains same.
I am wondering if it ispossible to control CPU usage to minimal through program
This problem becomes more serious when I run it on my webServer it hangs IIS and my site doesnot parse.
can some body helps me.
regards,
Arun
Code of my program is:-
***********************
import java.awt.*;
import java.awt.event.*;
public class test1 extends Frame implements ActionListener
{
TextArea text1=null;
TextField text2=null;
Button b1=null;
String abc="";
public test1()
{
setLayout(null);
text1=new TextArea(10,100);
text1.setBounds(10,60,100,100);
text2= new TextField();
text2.setBounds(10,40,100,20);
b1= new Button("Run");
b1.setBounds(100,180,50,20);
add(text1);
add(text2);
add(b1);
//text1.addActionListener(this);
text2.addActionListener(this);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent a)
{
/*if(a.getSource()==b1)
{
abc=text2.getText();
System.out.println("Text: "+abc);
for (int i=0;i<abc.length();i++)
{
//System.out.println("Text: "+i);
text1.append(""+abc.charAt(i));
text1.append("\n");
}
}*/
if(a.getSource()==b1)
{
abc=text2.getText();
int x=Integer.parseInt(abc);
System.out.println("Text: "+abc);
for (int i=0;i<x;i++)
{
//System.out.println("Text: "+i);
text1.append(""+i);
text1.append("\n");
}
}
}
public static void main(String str1[])
{
test1 xx=new test1();
xx.setSize(250,250);
xx.show();
}
}
**********************
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

You can always tell the thread to wait, like this:

Of course, the down side is that it will take longer to execute, but the CPU usage will go down considerably. Play with the sleep value a bit, and find a happy medium. Hope this helps!

Barry
Stanley Tan
Ranch Hand

Joined: May 17, 2001
Posts: 243
Use a lower thread priority.
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Thanks for your reply.
I tried it with setting the thread priority to min i.e. 1 but it does not make any difference.
Moreover this is not a thread program rather a simple application. Though I tried with the sleep method too it helps but if i set the value atleast 100 millisecond only.
Moreover the purpose here is to control the CPU usage? My only worry is if this is the case with such a simple regular program what will happen with other programs?
This all comes in my mind as my web servers IIS 5.0 used to get hanged which is running on windows 2000 and having a ram of 2GB and 40GB Harddisk. And everybody has a feeling it is because of this only?
Any opinion?
regards,
Arun
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
You can use Thread.yield() within your loop to let other threads have a chance to run.
One other thing to consider though is that if your system is idle in general, then the java VM may be hogging those idle cycles and giving your user Threads more attention than if the OS is really busy with a lot of processes and therefore the JVM isn't getting the same amount of processor time. Also, how is your performance configured...for background tasks or foreground applications? if you're running a machine as a server, make sure it's not setup to give foreground applications a bigger time slice.
One test would be to run two versions of your application, and monitor the usage times of both. IF your CPU is still at 100%, AND both applications are getting the same amount of processor time, then I would say the JVM is doing what it *should* be doing in that situation.
Rob
[ January 15, 2002: Message edited by: Rob Ross ]

Rob
SCJP 1.4
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The problem in this case is that the program itself is extremely inefficient, because it calls GUI-based methods far more often than it needs to. You know how people talk about the performance benefit of concatenating strings with StringBuffer rather than appending two Strings? The problem is much worse if you concatenate with TextArea.append(), because the system will try to graphically update the screen each and every time you call append. This program will run much, much, much faster if you create the whole string using a StringBuffer before you append it to the TextArea:

For the general question of how to limit CPU usage - setting thread priorities and using yield() can help to let other Java applications run in the same JVM at the same time - but they don't let you yield to processes outside the JVM. Furthermore it looks like a lot of the GUI functionality is really handled by native code, which in this case doesn't seem to be very good at yielding in a nice friendly matter. If you were running unix you could use "nice" to change the priority of a process - I don't know how to do this in Windows however. Ultimately, your best bet is to write code that doesn't need so much CPU time. Then yielding it won't be such an issue.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I control a CPU usages?