This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to kill a Thread which is reading Data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Java in General
Bookmark "How to kill a Thread which is reading Data" Watch "How to kill a Thread which is reading Data" New topic

How to kill a Thread which is reading Data

Sudarshan Muramreddy

Joined: Jun 08, 2004
Posts: 16
HI All,
Is there any way we can kill a thread which is reading data with out using stop method(i think Sun is going to depricate this method).
which typically could occur when user tries to read a file using java.IO API.

Fletcher Estes
Ranch Hand

Joined: Jul 01, 2004
Posts: 108
This would generally be done using the interrupt() method. If your thread is blocked on any I/O read/write and its interrupt method is called, a ClosedByInterruptException will be thrown, which you can catch and deal with gracefully in the thread.

You should take a look at the JavaDocs for the Thread class.
Anton Golovin
Ranch Hand

Joined: Jul 02, 2004
Posts: 476
Set a flag on your Runnable class and have the object instance check its copy periodically and exit if it is a certain value.

Anton Golovin ( SCJP, SCJD, SCBCD, SCWCD, OCEJWSD, SCEA/OCMJEA [JEE certs from Sun/Oracle]
I agree. Here's the link:
subject: How to kill a Thread which is reading Data
It's not a secret anymore!