File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Thread wait - wake on socket Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread wait - wake on socket" Watch "Thread wait - wake on socket" New topic
Author

Thread wait - wake on socket

Alex Garid
Greenhorn

Joined: May 04, 2011
Posts: 3
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

Joined: Oct 26, 2003
Posts: 396

Download a comms lib eg Netty, Grizzly, MINA etc there very multi threaded and the most common example is usually chat.


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3366
    
    9
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
Bartender

Joined: Oct 14, 2005
Posts: 18114
    
    8

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

Joined: May 04, 2011
Posts: 3
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

Joined: Oct 26, 2003
Posts: 396

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

Joined: Sep 20, 2010
Posts: 3366
    
    9
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

Joined: May 04, 2011
Posts: 3
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 ^^
 
wood burning stoves
 
subject: Thread wait - wake on socket
 
Similar Threads
Syncronising two classes
Threads and Socket streams.
Unable to read from the Socket Input Stream in a multithreaded environment with wait and notify
java.net Socket port number question
chat