Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Inconsistent CPU usage

Reply
Thread Tools

Inconsistent CPU usage

 
 
Elliot
Guest
Posts: n/a
 
      04-29-2008
Hi

Our Java Swing application uses sockets to communicate with a backend
Cobol database application. On 10% of the machines where the
application is running CPU usage goes to 100% at startup. On the
other 90% of the machines CPU usage is around 50%.

I develop on a P4 3.06 Gh Dell with XP SP2 and 1 gig of ram and run
at 50%. At one of our clients with the same P4 3.06 Gh and 2 gigs of
ram the application loads much slower and runs at 100%.

Perhaps the coding is part of the problem. A login screen is displayed
at the start of the application. In order to get the application to
stop and wait for user input the following line of code was used.

while(!pW.isFinished){;}; // programmatic block to put up screen

This waits (by looping) for the boolean to be set to true while the
password is verified via the socket connection with Cobol.

However, it takes many seconds to even get to this line. Java just
seems really slow to load.

I'm wondering how to understand the difference between the machines. I
also wonder if there are any java startup options that might help.

Both machines are using java "1.6.0_05" and we are loading a jar
file. Any ideas are most welcome

Thanks

Elliot
 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      04-29-2008
Elliot wrote:
> Hi
>
> Our Java Swing application uses sockets to communicate with a backend
> Cobol database application. On 10% of the machines where the
> application is running CPU usage goes to 100% at startup. On the
> other 90% of the machines CPU usage is around 50%.

....
> while(!pW.isFinished){;}; // programmatic block to put up screen


From the point of view of a CPU dispatcher, a dual threaded CPU chip,
with dual threading enabled, is two logical processors, as is a dual
core chip with both cores enabled. A single thread can be reported
as taking up to 1/P of the compute power, where P is the number of
logical processors in the system.

I think you should look very closely at the numbers of logical
processors enabled on each system. Probably, each of the systems
reporting 50% utilization has two logical processors, and each of the
systems reporting 100% has a single logical processor.

Patricia
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      04-29-2008
Elliot wrote:
> Hi
>
> Our Java Swing application uses sockets to communicate with a backend
> Cobol database application. On 10% of the machines where the
> application is running CPU usage goes to 100% at startup. On the
> other 90% of the machines CPU usage is around 50%.
>
> I develop on a P4 3.06 Gh Dell with XP SP2 and 1 gig of ram and run
> at 50%. At one of our clients with the same P4 3.06 Gh and 2 gigs of
> ram the application loads much slower and runs at 100%.
>
> Perhaps the coding is part of the problem. A login screen is displayed
> at the start of the application. In order to get the application to
> stop and wait for user input the following line of code was used.
>
> while(!pW.isFinished){;}; // programmatic block to put up screen
>
> This waits (by looping) for the boolean to be set to true while the
> password is verified via the socket connection with Cobol.
>
> However, it takes many seconds to even get to this line. Java just
> seems really slow to load.
>
> I'm wondering how to understand the difference between the machines. I
> also wonder if there are any java startup options that might help.
>
> Both machines are using java "1.6.0_05" and we are loading a jar
> file. Any ideas are most welcome
>
> Thanks
>
> Elliot


while(!pW.isFinished){;}; // programmatic block to put up screen

If the rest of your code looks at all like this, I'm surprised it runs
at all. You just cannot do this with Java and expect anything to work.

--

Knute Johnson
email s/nospam/linux/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
 
Reply With Quote
 
Christian
Guest
Posts: n/a
 
      04-29-2008
Elliot schrieb:

>
> while(!pW.isFinished){;}; // programmatic block to put up screen


Its no longer DOS you have Threads .. and you have several of them also
your OS might want some CPU power so its allways good to not use all at
least not for waiting.

If you want execution to just yield and wait for something best approach
would be to use wait()
use synchronize to get the monitor of an object then you can wait on it.
I would recommend you to exchange
while(!pW.isFinished){;};

with somthing like a call to a wait for finish method that could block
until finished (though call back might most times be better.. depends..)


the lazy method to fix your code would be sth like:
while(!pW.isFinished()) {
Thread.sleep(100);
};

if the finished variable is volatile or synchronized by some other means
this will work.

sun might have a learning trail for this.. GIYF
 
Reply With Quote
 
Elliot
Guest
Posts: n/a
 
      04-30-2008
On Apr 29, 4:59 pm, Patricia Shanahan <(E-Mail Removed)> wrote:
> Elliot wrote:
> > Hi

>
> > Our Java Swing application uses sockets to communicate with a backend
> > Cobol database application. On 10% of the machines where the
> > application is running CPU usage goes to 100% at startup. On the
> > other 90% of the machines CPU usage is around 50%.

> ...
> > while(!pW.isFinished){;}; // programmatic block to put up screen

>
> From the point of view of a CPU dispatcher, a dual threaded CPU chip,
> with dual threading enabled, is two logical processors, as is a dual
> core chip with both cores enabled. A single thread can be reported
> as taking up to 1/P of the compute power, where P is the number of
> logical processors in the system.
>
> I think you should look very closely at the numbers of logical
> processors enabled on each system. Probably, each of the systems
> reporting 50% utilization has two logical processors, and each of the
> systems reporting 100% has a single logical processor.
>
> Patricia


Thanks Patricia - it sure seems like a reasonable interpretation of
the usage percentages. We're checking it out
 
Reply With Quote
 
Elliot
Guest
Posts: n/a
 
      04-30-2008
On Apr 29, 5:46 pm, Knute Johnson <(E-Mail Removed)>
wrote:
> Elliot wrote:
> > Hi

>
> > Our Java Swing application uses sockets to communicate with a backend
> > Cobol database application. On 10% of the machines where the
> > application is running CPU usage goes to 100% at startup. On the
> > other 90% of the machines CPU usage is around 50%.

>
> > I develop on a P4 3.06 Gh Dell with XP SP2 and 1 gig of ram and run
> > at 50%. At one of our clients with the same P4 3.06 Gh and 2 gigs of
> > ram the application loads much slower and runs at 100%.

>
> > Perhaps the coding is part of the problem. A login screen is displayed
> > at the start of the application. In order to get the application to
> > stop and wait for user input the following line of code was used.

>
> > while(!pW.isFinished){;}; // programmatic block to put up screen

>
> > This waits (by looping) for the boolean to be set to true while the
> > password is verified via the socket connection with Cobol.

>
> > However, it takes many seconds to even get to this line. Java just
> > seems really slow to load.

>
> > I'm wondering how to understand the difference between the machines. I
> > also wonder if there are any java startup options that might help.

>
> > Both machines are using java "1.6.0_05" and we are loading a jar
> > file. Any ideas are most welcome

>
> > Thanks

>
> > Elliot

>
> while(!pW.isFinished){;}; // programmatic block to put up screen
>
> If the rest of your code looks at all like this, I'm surprised it runs
> at all. You just cannot do this with Java and expect anything to work.
>
> --
>
> Knute Johnson
> email s/nospam/linux/
>
> --
> Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
> ------->>>>>>http://www.NewsDemon.com<<<<<<------
> Unlimited Access, Anonymous Accounts, Uncensored Broadband Access


Knute - not really a helpful comment. How about pointing people in the
right direction?
 
Reply With Quote
 
Elliot
Guest
Posts: n/a
 
      04-30-2008
Hi Christian

Thanks for the friendly help and the ideas which I will research.

I wonder if anyone has example code to display a splash screen with
input fields and which then goes on to display the main application. .

Cheers

Elliot
 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      04-30-2008
Elliot wrote:
> On Apr 29, 5:46 pm, Knute Johnson <(E-Mail Removed)>
> wrote:
>> Elliot wrote:
>>>
>>> Our Java Swing application uses sockets to communicate with a backend
>>> Cobol database application. On 10% of the machines where the
>>> application is running CPU usage goes to 100% at startup. On the
>>> other 90% of the machines CPU usage is around 50%.

....
>>> Perhaps the coding is part of the problem. A login screen is displayed
>>> at the start of the application. In order to get the application to
>>> stop and wait for user input the following line of code was used.
>>> while(!pW.isFinished){;}; // programmatic block to put up screen
>>>

>> while(!pW.isFinished){;}; // programmatic block to put up screen
>>
>> If the rest of your code looks at all like this, I'm surprised it runs
>> at all. You just cannot do this with Java and expect anything to work.
>>

>
> Knute - not really a helpful comment. How about pointing people in the
> right direction?


Without seeing the rest of the code it is hard to be more specifically
helpful. Not since the days of single tasking DOS has it been sensible
to burn CPU cycles in a tight loop as a method of waiting.

With a Swing application you should never need to do this, the whole
thing should by default be event driven.

e.g.
String pw = JOptionPane.showInputDialog(frame, "Enter Password");
new SocketyThing().start(pw);

The second statement won't be run until the first statement has been
completed (by user pressing "OK").

A more realistic example would put SocketyThing().start(pw) into a
SwingWorker - still no need for explicit waiting or signalling.

HTH.

--
RGB
 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      04-30-2008
Elliot wrote:
> Hi Christian
>
> Thanks for the friendly help and the ideas which I will research.
>
> I wonder if anyone has example code to display a splash screen with
> input fields and which then goes on to display the main application. .
>


Here's some rubbish I dashed off in a couple of mins. It compiles, runs
and does what I expected it to do.

---------------------------- 8< ----------------------------
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;


public class PwSocket {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new PwSocket();
}
});
}

PwSocket() {
final String pw = JOptionPane.showInputDialog("Password");

new SwingWorker<Void,Void>() {
@Override
protected Void doInBackground() throws Exception {
new SocketThing(pw).start();
return null;
}
}.execute();

JPanel p = new JPanel();
p.add(new JLabel("Your App goes here"));

JFrame f = new JFrame("Passworded Socket");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

class SocketThing {
SocketThing(String pw) {
// Use pw to init stuff
}
public void start() {
// Network I/O here (off EDT)
}
}

}
---------------------------- 8< ----------------------------

HTH

--
RGB
 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      04-30-2008
RedGrittyBrick wrote:
> Elliot wrote:
>> Hi Christian
>>
>> Thanks for the friendly help and the ideas which I will research.
>>
>> I wonder if anyone has example code to display a splash screen with
>> input fields and which then goes on to display the main application. .
>>

>
> Here's some rubbish I dashed off in a couple of mins. It compiles, runs
> and does what I expected it to do.
> [snip]




I decided to use this as an opportunity to learn about java.net.Socket.

---------------------------- 8< ----------------------------
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

public class PwSocket {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new PwSocket();
}
});
}

private JLabel label = new JLabel("Busy...");

PwSocket() {
final String pw = JOptionPane.showInputDialog("Password");

final SocketThing socketThing = new SocketThing(pw);

final JFrame f = new JFrame("Passworded Socket");

new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
socketThing.start();
return null;
}
@Override
protected void done() {
if (socketThing.hasError()) {
JOptionPane.showMessageDialog(f,
socketThing.getErrorMessage());
System.exit(1);
} else {
label.setText("["+socketThing.getData()+"]");
}
}
}.execute();

JPanel p = new JPanel();
p.add(label);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

class SocketThing {
private Socket sock;
private PrintWriter out = null;
private BufferedReader in = null;

private String errorMessage;
private String data;

SocketThing(String pw) {
System.out.println("Creating SocketThing...");
if (!pw.equals("Foo")) {
errorMessage = "Bad password!";
} else {
try {
sock = new Socket("www.google.com", 80);
out = new PrintWriter(sock.getOutputStream(), true);
InputStream is = sock.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
in = new BufferedReader(isr);
} catch (UnknownHostException e) {
e.printStackTrace();
errorMessage = e.getMessage();
} catch (IOException e) {
e.printStackTrace();
errorMessage = e.getMessage();
}
}
System.out.println("SocketThing created.");
}

public void start() {
System.out.println("Starting SocketThing...");
if (out != null) {
out.println("GET / HTTP/1.1");
out.println("");
try {
data = in.readLine();
} catch (IOException e) {
e.printStackTrace();
errorMessage = e.getMessage();
return;
}
}
System.out.println("SocketThing finished");
}

public boolean hasError() {
return errorMessage != null;
}

public String getErrorMessage() {
return errorMessage;
}

public String getData() {
return data;
}
}

}
---------------------------- 8< ----------------------------


--
RGB
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the difference between Memory Usage and Heap Usage in my JVMMetrics ? Krist Java 8 02-10-2010 12:44 AM
retrieving CPU Usage and Memory Usage information in JAVA hvt Java 0 03-13-2007 01:09 PM
retrieving CPU Usage and Memory Usage information in JAVA hvt Java 0 03-13-2007 01:07 PM
Webchecker Usage - a problem with local usage Colin J. Williams Python 1 02-26-2004 12:28 AM
Need help on memory usage VS PF usage metfan Java 2 10-21-2003 01:58 PM



Advertisments