This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma 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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
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

How to minimize CPU Usage for Sockets and Threads

Zach Stallbohm

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%*


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


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

Joined: Sep 20, 2010
Posts: 4637

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.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
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.

Avi Stramer

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

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

Oracle Monitoring Tools : (jps, jstat et jstatd)

Oracle Troubleshooting Tools : (jinfo, jhat, jmap, jsadebugd, jstack)

VisualVM :

Eclipse Tptp :

Memory analyser :
I agree. Here's the link:
subject: How to minimize CPU Usage for Sockets and Threads
It's not a secret anymore!