aspose file tools*
The moose likes Threads and Synchronization and the fly likes need help on good multithreading practices Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "need help on good multithreading practices" Watch "need help on good multithreading practices" New topic
Author

need help on good multithreading practices

joseph dela cruz
Ranch Hand

Joined: May 17, 2012
Posts: 38

i have a very simple chat app. its not done yet but whats bothering me is when i am creating a thread for an update of GUI asap it uses both cores at 100% i do not have air condition but its not that hot here in the philippines.
does 100% cpu usage burns my proccessor plus im using htop in terminal in ubuntu at first loading of my application it sets both cores to 100% then when i try to stop and restart my mini chat app server it goes 90% and below but its red.
does it mean my cpu are working more than it should be?
plus i observed that some of the games(heavier than my mini chat app of course) that i play. doesnt use that much cpu please help



this is my thread for my gui update

and this my code for stop and start server button


by the way if im doing something wrong please tell me thanks
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

Hi joseph,

Are you sure that you are using an empty infinite loop for GUI refresh? Any specific reason for that? I guess that this might be the reason behind 100% CPU utilization (though I'm not getting why it should blow up both cores)
Also, is it really necessary to refresh GUI in real-time? How about sleeping for, say, 500 milliseconds?

Besides that, for this specific problem, I would like to recommend Java Network Programming by Harold. Its pretty nice book, and I referred to it when I wanted to develop a small chat system - where server would entertain multiple clients in multithreaded environment and so on.

For threading best practices, I would suggest below books:
1)Java Concurrency in Practice by Bloch et.al. An excellent book in understanding Java threading framework in general. Also discusses some threading issues as well.
2)Java Threads by Wong et.al. If you are new to Java threads, this should be your first book. Easy to understand, practical and very handy.
3)Taming Java Threads by Holub. This should be referred when you are developing real life multithreaded applications and want to squeeze maximum output off Java threads.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
joseph dela cruz
Ranch Hand

Joined: May 17, 2012
Posts: 38

Anayonkar Shivalkar wrote:Hi joseph,

Are you sure that you are using an empty infinite loop for GUI refresh? Any specific reason for that?

that block is not empty i just did not put it to make the code shorter but what it does is disable and enable a terminateButton on a specific connection if the list is empty then it will go disable anyway thanks for the book
Avi Stramer
Greenhorn

Joined: Aug 23, 2012
Posts: 6

Post all the code including what you are doing in the infinite while loop as well as the other loop. Pretty much as a certainty that the problem is there.
Kapish M Joshi
Greenhorn

Joined: Sep 12, 2012
Posts: 10

Hi,

It seems that you want to scan incoming messages from server and show them on UI as soon as you get it. You might have written this code at client side application:



Here the problem is, there is a single thread trying to run same code in unending loop. For example lets say you are creating a HashMap inside a code, it mean HashMap creation will happen for n times until memory is out or program crashes. Instead you should try keeping code in thread such that it doesn't have infinite loop rather it should complete its operation and end. Try to spawn new tread each time for doing such operations after certain interval of time.

Apart from this, you have done nice job by creating singleton server thread instance, but make sure you should rarely use deprecated methods. return type of stop() & start() methods is void. Please modify your method signature accordingly.



Kapish M Joshi| kjoshi@infocepts.com | InfoCepts
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: need help on good multithreading practices