Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread wait - wake on socket

 
Alex Garid
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,

I found this forum while googling for a solution about an issue i am having.
I am starting a client to communicate with somewhere outside with only purpose of... well of getting some experience with it.
I have decided to have two threads running, one that would threat the data from the socket's output stream, when there is, the other for the socket's input stream / stdin.

My purpose is simple: The client is a simple chat program therefore there is nothing to process, send and receive data. Having that in mind, i do not wish to waste cpu processing time when there is no input/output. I am familiar with wait / notify, also have some background about mutexes and semaphores but i don't see how those would help. I have tried to find out how can i do the following: Having both threads sleeping, if the socket receives new data to show/send, it wakes one thread (or both). Thread(s) do(es) its thing and goes back to sleep. I don't know how to make socket trigger the threads though.

I'm sure someone has thought of that before, our own browsers and chat programs aren't wasting unnecessary resources while(!noInput){ busy_waiting; } :P

Thanks for the attention
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Download a comms lib eg Netty, Grizzly, MINA etc there very multi threaded and the most common example is usually chat.
 
Stephan van Hulst
Bartender
Pie
Posts: 5897
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alex, welcome to CodeRanch.

I don't think you actually have to do anything special. You can use a handler thread that reads from the socket when data becomes available. Usually any methods that do this will block without wasting resources. For writing data, you can just do this when your main program deems necessary. You don't need a separate thread for this.

If your main program needs to wait for data coming from the input before it writes anything out (or has to do other operations), you can use the wait() and notifyAll() methods defined in Object.
 
Paul Clapham
Sheriff
Posts: 21133
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the Custom Networking tutorial, especially the "All About Sockets" section. And especially the little bit at the end headed "Supporting Multiple Clients".

Like Stephan said, you don't have to mess with wait/notify or semaphores or mutexes or any of that.
 
Alex Garid
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good morning,

Stephan van Hulst wrote:Hi Alex, welcome to CodeRanch.

Thanks, i'll take some time to check the forum better soon enough

Chris Hurst, i am willing to do some "hardcore programming" instead of using platforms already built, as i mentioned earlier it's not a real project and more of a learning experience while i have some free time :P Thanks for your reply though

Also, i got my issue solved thanks to Stephan and Paul, i had failed to understand that reading from the socket's inputstream would cause the desired effect: wait for socket to have something to be read. I was therefore, overloading CPU constantly verifying if the socket had any data to be read.

Thank you everyone
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok if your going hard core ;-) you want to be looking at Java NIO ... Java non blocking IO (sometimes called Java native IO) but its not trivial.
 
Stephan van Hulst
Bartender
Pie
Posts: 5897
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, NIO stands for New I/O

It's a different I/O scheme that uses channels instead of streams. But yeah, socket channels can be either blocking or non-blocking. Actually, there's a nice feature that will wake up your handler when any one of many different sockets receives input.
 
Alex Garid
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You raised my curiosity about NIO and i went read some about it, seems pretty interesting yes :P Thanks for the suggestion! I was not aware that default I/O was considered slow nowadays, mostly because i suppose it uses OS-based calls and these were improved over time. Guess it does more than i thought ^^
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic