Home arrow static arrow Java Programming [Archive] - java.exe - power hungary program?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - java.exe - power hungary program?
This topic has 10 replies on 1 page.

Posts:70
Registered: 16/9/03
java.exe - power hungary program?  
Jul 10, 2004 5:20 PM



 
Ok, this is a very weird problem (I think). A few times now, I've run a java program (using java <classname>) and my computer (top of the line Dell Inspiron 9100, very fast, 1 gb of RAM) has slowed considerably. To put it in perspective, I've rendered a video using Premier, at the same time as playing Counter-Strike, with no noticable decline in performance. Now, for the real proof, I've used Task Manager every time to find the problem. Every time java.exe has been using greater then 95% of my CPU. It has Hyper Threading, and Both CPU's are hitting 100%. It's a very fast jump from normal operating to ~100%, and it'll platue there, until I close the java.exe process. I can't imagine why it's doing this, can you? Should I be using any sort of arguments in the command. I also (the last time, I can't remember the others) was using a batch file to run my program. Will THAT effect anything? (I've never in my years dealing with computers, seen a shortcut/batch file clobber the CPU).
Ok, the other program factors. At the time this happened, I was running a Server I wrote for a game I'm making. I was also running the client (both are threaded) at the same time (well duh, when else would I run the client *thwap*). I can provide code for both, if need be, they're short enough. I have run both at the same time before, and did here for about two minutes before I noticed the CPU acting up.
Any thoughts are appreciated, solutions even more so :) Thanks for your time!
 

Posts:9,091
Registered: 8/15/01
Re: java.exe - power hungary program?  
Jul 10, 2004 5:47 PM (reply 1 of 10)



 
Are you up to day (pref. 1.5 beta, but atlest 1.4)

Does this happen with ever Java app (try some stable apps, like Apache Tomcat, Azureus, NetBeans, Elispse etc).

If not, give an out line of your app in psudo code/code dumps of the application.
 

Posts:19,725
Registered: 9/26/01
Re: java.exe - power hungary program?  
Jul 10, 2004 5:49 PM (reply 2 of 10)



 
Then wouldn't the logical conclusion be that <classname> is a buggy app, which is going into a tight loop or something, eating up CPU time? Is that A) Java's fault, or B) the author of <classname>'s fault? Answer: B
 

Posts:70
Registered: 16/9/03
Re: java.exe - power hungary program?  
Jul 10, 2004 6:04 PM (reply 3 of 10)



 
I was assuming that it was the "author of <classname>'s fault, namely, me. But the circumstances are weird, and I can't duplicate them. Here's an update.
I just re-ran the combination of my two programs, and while one spiked to 73% when it loaded (the GUI client), neither of them took much CPU. It ran at about 15% the entire time kI was testing (and running some music, and browser). This is completly normal. So, I tested using multiple copies of the Client, multiple servers, and neither had the desired (or rather, undesired) effect. I tested running one from jGRASP< one from the Batch file. I tested both from the batch file, no luck. I'm completely stumped. Yes, I am running the latest 1.4, not going up to 1.5 (or should I say 5, or perhaps Tiger?) until it's out of Beta. This has happened, by my account, 4 times in the past, and it's always been with classes I use a lot. Now it's seemin to me that its some weird case that my classes are just eating java.exe, but I can't imagine how. Here's my two thread (i.e. the classes, minus the initializers).
    public class Server implements Runnable   {       public void run()      {         while(connected)            try            {               socket = serverSocket.accept();               socket.setSoTimeout(200);               new Player(socket, me);               numberOfPlayers++;               System.out.println("New player " + numberOfPlayers + "!!\n" + socket.getInetAddress());            }                catch(IOException e)               {                  e.printStackTrace();               }      }}    public class Player implements Runnable   {       public void run()      {         while(socket.isConnected())         {            try            {               String toPrint = input.readLine();               System.out.println(toPrint);            }                catch(IOException e)               {               }         }      }}

No, so far I don't actually talk to the Player class, it just creates itself. The Client class is just a GUI that accepts input from a Menu, or textField (not implemented yet, save to copy the text to a Text Area). If you select connect, you connect to my server, and it asks for a name. That's all I have so far, and I can't see that either of the threads would cause a problem. ANy suggestions appreciated. Thanks
 

Posts:447
Registered: 3/8/01
Re: java.exe - power hungary program?  
Jul 10, 2004 6:17 PM (reply 4 of 10)



 
How are you handling a client exiting? I think the isConnected method does not return false unless you try to write to the other side and it fails. Try printing the stack trace of the IOException that you catch, and see whether you're in an infinite loop. That would certainly be a problem.

Also, I don't see why you need setSoTimeout. You're reading input in a loop anyway, and it doesn't matter whether readLine() just blocks for a long time or whether it throws an exception every 200 ms and you call it again after. This shouldn't be a big performance problem though.
 

Posts:70
Registered: 16/9/03
Re: java.exe - power hungary program?  
Jul 10, 2004 8:28 PM (reply 5 of 10)



 
Ok, it's not the client exiting that breaks it, it's just running that throws an exception. Here's what I got when I ran the program and connected once.
java.net.SocketTimeoutException: Read timed out	at java.net.SocketInputStream.socketRead0(Native Method)	at java.net.SocketInputStream.read(SocketInputStream.java:129)	at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(StreamDecoder.java:408)	at sun.nio.cs.StreamDecoder$CharsetSD.implRead(StreamDecoder.java:450)	at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:182)	at java.io.InputStreamReader.read(InputStreamReader.java:167)	at java.io.BufferedReader.fill(BufferedReader.java:136)	at java.io.BufferedReader.readLine(BufferedReader.java:299)	at java.io.BufferedReader.readLine(BufferedReader.java:362)	at Player.run(Player.java:37)	at java.lang.Thread.run(Thread.java:534)

I don't know if it's an inifinite loop in the term that while(true) is, but it is an infinite error. How can I fix this? (oh, that error was made by the player class)
       public void run()      {         while(socket.isConnected())         {            try            {               String toPrint = input.readLine();/*Line 37*/               System.out.println(toPrint);            }                catch(IOException e)               {                  e.printStackTrace();               }         }      }

I have the setSoTimeout because some code that I had looked at for reference said I needed to set it so the server wouldn't crash when it's not busy. I just ran the program without the setSoTimeout, and it works much better. However, once I exit, now I start a connection reset error, which at least I can deal with. How should I do that loop (right now I changed it to while(true))? Thanks for your help, and Matei great comments! It's leading to a solution!.
 

Posts:70
Registered: 16/9/03
Re: java.exe - power hungary program?  
Jul 10, 2004 8:29 PM (reply 6 of 10)



 
Sorry if the post above is hard to follow, but I did it while I implemented everything in your response, and what it brought to mind, just so I could say I tried it all.
 

Posts:70
Registered: 16/9/03
Re: java.exe - power hungary program?  
Jul 10, 2004 8:37 PM (reply 7 of 10)



 
Also, I don't see why you need setSoTimeout. You're
reading input in a loop anyway, and it doesn't matter
whether readLine() just blocks for a long time or
whether it throws an exception every 200 ms and you
call it again after. This shouldn't be a big
performance problem though.
Actually, I'm an idiot. this WAS the performace problem (as I was trying multiple clients, and the server threw an exception for each ever 200 milliseconds). This has been a great lesson to me. I am NEVER referencing other's code again, only the API. I was curious just now, trying to figure out a better boolean, saw the setSoTimeout, read the description, and hit myself over the head. Had I read up, I would have never used that method.
 

Posts:447
Registered: 3/8/01
Re: java.exe - power hungary program?  
Jul 11, 2004 5:34 AM (reply 8 of 10)



 
Great to see that you found the problem. By the way, if you're expecting to get many clients, it's better to use java.nio ("New I/O") instead of java.io, because it allows you to keep everything in a single thread by using non-blocking read's. Switching between threads becomes a big performance hit when you have many clients, and multithreaded programming is tricky due to synchronization. Unfortunately, there's no NIO tutorial from Sun (that I can find), but here are two links I found useful:
- http://www.javaworld.com/javaworld/jw-09-2001/jw-0907-merlin.html (a simple HTTP server using NIO.. actually, they use two threads, but it can also be done with one).
- http://grexengine.com/sections/people/adam/nio/Introduction_to_NIO_Networking.html (three articles on NIO for games; unfortunately, they're a bit unfinished, but they explain the concepts pretty well).
Also, something not mentioned in those articles is that you can use selectNow() for a non-blocking select() to really keep your whole game loop in a single thread.
 

Posts:441
Registered: 2/25/04
Re: java.exe - power hungary program?  
Jul 11, 2004 6:01 AM (reply 9 of 10)



 
Currently, 65% of Hungary's primary energy supplies are imported from Russia alone. Only a small portion of Hungary's coal reserves can be mined economically, and today most coal is mined by companies that are integrated with coal-fired electricity generating companies. Supplementing locally mined coal, 860,000 tons of coal were imported in 1996. Hungary's total installed electricity generation capacity is 7,510 MW. Of the total 37 TWh demand for electricity in 1996, 38.4% was generated by the only nuclear power station at Paks; large coal-fired generators produced 26% and oil and gas 28%; 2% was generated by small independent power producers; and 6% of the domestic demand was imported. Of Hungary's eight generating companies, the three largest, including the nuclear plant, account for 70% of the country's total installed capacity. Most of the fossil fuel generating plants are old, uneconomic and polluting.

The Hungarian energy sector's long-term development plans call for the construction of up to 6,000 MW of new capacity and the scrapping of some 2,000 MW of old plants by 2010. Privatisation of the energy utilities in Hungary started in 1995. International strategic investors who have bought shares in the electricity generating companies include companies from Germany, Belgium, Finland, Japan and the USA. Owners of minority shares in the six electricity distribution companies include companies from France and Germany.


Don't see much about Java being part of the program to power Hungary.
 

Posts:70
Registered: 16/9/03
Re: java.exe - power hungary program?  
Jul 11, 2004 11:14 AM (reply 10 of 10)



 
Thanks VERY much for those excellent links. I'm just reading through the first and it's exactly what I need/want. I decided that this did deserve dukes, since you helped me debug the code as well as point me to the better source!
 
This topic has 10 replies on 1 page.