This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
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
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.
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.
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.