• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

need help on good multithreading practices

 
Ranch Hand
Posts: 38
Oracle Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
joseph dela cruz
Ranch Hand
Posts: 38
Oracle Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 6
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 10
Hibernate Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


reply
    Bookmark Topic Watch Topic
  • New Topic