This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes interrupt() for ending thread execution and asynchronous i/o Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "interrupt() for ending thread execution and asynchronous i/o" Watch "interrupt() for ending thread execution and asynchronous i/o" New topic
Author

interrupt() for ending thread execution and asynchronous i/o

Saurabh M Sirdeshmukh
Greenhorn

Joined: Feb 11, 2009
Posts: 10
Guys, I came across many apis suggesting that interrupt() is d best way to terminating a thread execution especially those performing blocking operations. i pretty much agree with it n also helpful for doing any clean-up act before ending thread execution. but what i e wanna ensure from all you guys is: how actually that thread wil end? is it by returning from the run()? becoz stop() n destroy() are to b ultimately avoided...


2nd: i believe I/O in java is synchronous or blocking...if there is just a single thread n it does read() in System.in then it wil definitely block.if i wanna implement it asynchronousely then I can do this reading in another thread while doing some work in current thread?
Is this approach right?
lemme know



Another Brick In The Wall!
SCJP 5.0/SCMAD 1.0
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18505
    
  40

Guys, I came across many apis suggesting that interrupt() is d best way to terminating a thread execution especially those performing blocking operations. i pretty much agree with it n also helpful for doing any clean-up act before ending thread execution. but what i e wanna ensure from all you guys is: how actually that thread wil end? is it by returning from the run()? becoz stop() n destroy() are to b ultimately avoided...



When you interrupt a thread -- it doesn't just end. It gets the interrupt signal, and your thread must deal with it. It is your thread's responsibility to clean up after itself.... Meaning....

1. If the thread gets interrupted while running, it will get marked as interrupted. It is the threads responsibility to check the flag, once in a while (if it is compute intensive), and abort the calculation and clean up after itself.

2. If the thread gets interrupted while in a wait(), sleep(), join(), etc., it will get an interrupted exception. Again, it is the threads responsibility to abort whatever it is doing and clean up after itself.

3. If the thread gets interrupted while doing IO, it is platform independant. On some systems, you will get an interrupted IO exception. On other systems, it won't work by itself. You also need to close the network or file that the thread is using. In this case, the thread will get an IO exception and can then check the interrupted flag.... And again, it is the threads responsibility to abort whatever it is doing and clean up after itself.




2nd: i believe I/O in java is synchronous or blocking...if there is just a single thread n it does read() in System.in then it wil definitely block.if i wanna implement it asynchronousely then I can do this reading in another thread while doing some work in current thread?
Is this approach right?
lemme know


Yes, threading is one solution to have your program not wait for a blocking IO operation. Another option is to using non-blocking IO via the NIO package.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Arka Guhathakurta
Ranch Hand

Joined: Mar 01, 2009
Posts: 46
Henry you are an extremely knowledgable as well as talented individual....I wish I could be 1% of you.


regards,
Arka
Saurabh M Sirdeshmukh
Greenhorn

Joined: Feb 11, 2009
Posts: 10
Hey Henry, thanks a lot.
and can you please tell me what does the terms: "synchronous" n "asynchronous" in context of exceptions.
and how these terms are expected to be interpreted in background of java threads?
n by the way are you the same Henry Wong, author of Java threads book? privilig to here from you
thanks.
Alan Mehio
Ranch Hand

Joined: Apr 04, 2005
Posts: 73

Guys, I came across many apis suggesting that interrupt() is d best way to terminating a thread execution especially those performing blocking operations. i pretty much agree with it n also helpful for doing any clean-up act before ending thread execution. but what i e wanna ensure from all you guys is: how actually that thread wil end? is it by returning from the run()? becoz stop() n destroy() are to b ultimately avoided...

Just to add more details by giving example to show who we deal with interruption







2nd: i believe I/O in java is synchronous or blocking...if there is just a single thread n it does read() in System.in then it wil definitely block.if i wanna implement it asynchronousely then I can do this reading in another thread while doing some work in current thread?
Is this approach right?
lemme know


The synchronous means here the thread can not move forward ( serial behaviour) unless the method which is a blocking method (reading or writing to input or output Stream are not responsive to interruption in java.io package) so you the only thing you can do if it is a socket or a stream is to close it ( you will have a reference to )

In the asyncrhonouse situation where you do not want to see the effect of block so If a thread is blocked in a Selector.select(java.nio.channels)method, the
Selector.wakeup method causes it to return prematurely by throwing a ClosedSelectorException
so this will make the thread to work in an asynchronous behaviour





Regards,
Alan Mehio
London, UK
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: interrupt() for ending thread execution and asynchronous i/o
 
Similar Threads
What is meant by Blocking I/O ?
When is lock released?
Q on thread at Jxam
Thread Intrerupt
Exam on monday please reply