This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Performance and the fly likes How to minimize CPU Usage for Sockets and Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Performance
Bookmark "How to minimize CPU Usage for Sockets and Threads" Watch "How to minimize CPU Usage for Sockets and Threads" New topic
Author

How to minimize CPU Usage for Sockets and Threads

Zach Stallbohm
Greenhorn

Joined: Aug 15, 2012
Posts: 1
I've searched the site already to try and make sure this isn't a repost but if it is I'm greatly sorry (my first post).

I created an instant messenger using java. When I have the Server that communicates between the clients and one client running on my Computer the CPU Usage is at 100%. It really slows down everything else I'm doing and I figure this might be an issue if I gave this to people to use. I don't want the client taking up a lot of CPU Usage if they're just running it in the background while doing other things on their computer. The program utilizes multithreading. Each thread is constantly being polled for input.

The Server, as seen below, has two threads. I explain what the threads do before the code. There is also another while loop running constantly in the server that is waiting for sockets to connect. The loop does not run constantly at the line socket.accept(); it stops and just waits.

The User, split into a menu and chat window, has two threads as well. I explain what the threads do before the code.

*Edit: After I originally posted I put a 100 ms sleep in all my threads. CPU Usage is still at 100%*

Server

This thread listens for input from the user. The input tells the server what action to take. There is a thread running for every user currently connected to the server.


This thread waits for input between the two user's who are currently chatting


UserMenu

This thread listens for input from the server and updates the current list of who's online. It also listens for if someone wants to chat so it automatically opens up the chat window


Chat Window:

This thread listens for input from the other user you're chatting with


*Rewrote into an organized paragraph*

If you want to see the code please ask because the code for each is rather lengthy.

Thank you for your time
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

Hi Zach, welcome to CodeRanch!

It sounds like you have a "busy wait" somewhere. A busy wait is a loop which constantly polls for a condition to change, instead of going to sleep and waiting for a notification that the condition changed.

Find the loop which runs constantly, and then post the code for that. You can find the busy loop by putting println() calls in your loops with a diagnostic message, and then if it prints a lot of messages for one loop even though the program appears to be doing nothing, that's the loop you'll want to improve.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12791
    
    5
Incidentally, writing in organized paragraphs instead of "stream of consciousness" will make it easier for us to understand your problem and be good tech writing practice for you.

Bill
Avi Stramer
Greenhorn

Joined: Aug 23, 2012
Posts: 6

I/O calls on socket input streams block, so it is much more efficient to just call .read() and let it block waiting for data than to constantly check input.available() > 0 and otherwise sleep 100ms. By using the blocking call, it'll return as soon as there is any data inbound on the socket.

That said, this whole approach will not scale since you are using blocking I/O and WAY too many threads. A better approach would be to use NIO sockets (google it). Keep track of a HashMap from username -> SocketChannel and on the NIO Selector thread process the requested chat operation. This is much more efficient and can scale much higher since you aren't keeping so many threads open. I left out a lot of details, but might be enough to get you started if you want to go down this road.

Good luck!
Jony Coxy
Greenhorn

Joined: Nov 01, 2013
Posts: 2
Free tools :

Oracle Monitoring Tools : http://download.oracle.com/javase/6/docs/technotes/tools/#monitor (jps, jstat et jstatd)

Oracle Troubleshooting Tools : http://download.oracle.com/javase/6/docs/technotes/tools/#troubleshoot (jinfo, jhat, jmap, jsadebugd, jstack)

VisualVM : https://visualvm.dev.java.net/

Eclipse Tptp : http://www.eclipse.org/tptp/

Memory analyser : http://www.eclipse.org/mat/
 
wood burning stoves
 
subject: How to minimize CPU Usage for Sockets and Threads