This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Thread Eats Processor Power

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey everyone. I'm new to Java, so I thought a cool way to learn would be to write a chat client. I'm happy to report that it's going pretty good, except for one major problem.

When I'm ready for the program to connect, it'll start up a thread for handling socket communications. The thread will start by sending the initial handshake and enter a loop for incoming messages. The problem is that it starts consuming processor avalibility, so that nothing else will work until I exit the program. How do I prevent this from happening?

Thanks!
 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how are you reading these messages from the other end? in most cases, a network client/server program will use a read() or readLine() or readObject() method on some stream or Reader object, and these methods will block quietly until there's input to be had. why aren't yours blocking?
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the code that performs the reading? That would increase help us to help you.

I do have a guess, though. Are you calling available() on the Socket's InputStream inside a loop until it returns a value greater than zero? This would cause the thread to greedily eat up CPU cycles until some data arrived.

Replace the loop and available() call with a call to one of the read() methods. If there is no data available, the thread will sleep (no CPU time taken) until data arrives. At that point the JVM wakes the thread so it can continue with the read() call.
 
Curse your sudden but inevitable betrayal! And this tiny ad too!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic