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 regarding Thread.stop() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "regarding Thread.stop()" Watch "regarding Thread.stop()" New topic
Author

regarding Thread.stop()

dharmesh baveja
Greenhorn

Joined: Dec 18, 2000
Posts: 13
Hi,
I need some help regarding Thread.stop() method of java.lang.Thread Class.
My Application uses Thread.stop() method for killing the Thread and this method is deprecated in JDK1.2.2 . I am looking for the alternate so same functionality.
Scenario of Application:
There are three classes
1.Class MyThread : This class extends java.lang.thread . In the run() method some URL Connection is opened and inputStream is fetched . There is no problem regarding inconsistent state of objects due to terminating Thread during execution of run() or I/O (Thread.stop() is not at all unsafe for this situation). There is no while or for loop in run() method . Thread goes thru run only once and this may take enough time in opening URLconnection .
2.Class Creator extend Thread: This class creates a new Thread as and when required and stores Thread reference and time of creation of thread in some Vector and starts the Thread simultaneously.
3.Class Monitor extend Thread: It gets the Thread reference and there time of time of creation from the same Vector . Compare the timestamp of thread and current time. If the time difference is more than desired limit( Thread goes thru run only once and this may take enough time in opening URLconnection ) , the thread is killed there .
Requirements are:
1.run method() of above three classes is executed simultaneously.
2.There is delay in URLConnection . If possible, Thread spending too much time in Connection should be killed ,ie. , We should wait for Statement to be executed and then killing the thread .
3.There is no for or while loop in any of run() method in classes . The approach of setting a Boolean falg and checking that in while loop will not help.
Pls help me out of this .
Similarly i am also using Thread.countStackFrames(). Help on that also needed.
thanks in advance.
Dharmesh
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by dharmesh baveja:
[...] In the run() method some URL Connection is opened and inputStream is fetched . There is no problem regarding inconsistent state of objects due to terminating Thread during execution of run() or I/O (Thread.stop() is not at all unsafe for this situation).

Hmmm. Are you very sure about that?
Anyway, you may want to experiment with Thread.interrupt().
- Peter
dharmesh baveja
Greenhorn

Joined: Dec 18, 2000
Posts: 13
Peter,
Can u please give me some more detail/hints about the approach you have in your mind.
I am sure that none fo my objects will be in incosistent state.
Secondly , as i think Thread.Interrup() is to awake sleeping threads.
Pls tell indetail.
Junaid Bhatra
Ranch Hand

Joined: Jun 27, 2000
Posts: 213
Dharmesh,
You are right, interrupt() only works on sleeping or waiting threads. It does not affect running threads, apart from setting the interrupted status flag to true, and after that if the thread enters the sleeping or waiting state, it would immediately raise an InterruptedException.
In your particular scenario, I cannot think of a standard solution to this problem. You can try closing the Socket or URLConnection input stream, which may cause the thread to return immediately. The following information might help, which I obtained from the API.

What if a thread doesn't respond to Thread.interrupt?
In some cases, you can use application specific tricks. For example, if a thread is waiting on a known socket, you can close the socket to cause the thread to return immediately. Unfortunately, there really isn't any technique that works in general. It should be noted that in all situations where a waiting thread doesn't respond to Thread.interrupt, it wouldn't respond to Thread.stop either. Such cases include deliberate denial-of-service attacks, and I/O operations for which thread.stop and thread.interrupt do not work properly.

Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by dharmesh baveja:
Secondly , as i think Thread.Interrup() is to awake sleeping threads.

Hmmm. From hints elsewhere and the Java documentation I had the impression that socket connections could be interrupted, throwing an java.io.InterruptedIOException, which is a subclass of java.io.IOException. In fact the JavaDoc for this exception explicitly refers to Thread.interrupt().
Well, even if the documentation leaves the possibility open, it has certainly not been implemented I tried and I was wrong.
Sorry.
- Peter
dharmesh baveja
Greenhorn

Joined: Dec 18, 2000
Posts: 13
hi all ,
Please help me out of this problem . I am just stuck up in my work because of these . Cordinators please if u can expedite it , if possible . Thanks to all for ur replies!!!
Dharmesh
Peter Haggar
author
Ranch Hand

Joined: Jan 03, 2001
Posts: 106
Here is one idiom that works using Thread.interrupt().

However, you said that you don't have a for or while loop in your run() method. Without one, you would have to code specific checks for isInterrupted and return if it returns true. For example:

Neither of these techniques gives you the ability to stop the thread exactly when you want to. If you call Thread.interrupt(), the thread won't stop until you exit the run method and that won't happen until you call isInterrupted(), then return.
Peter Haggar
------------------
Senior Software Engineer, IBM
author of: Practical Java


Senior Software Engineer, IBM
author of: Practical Java
Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324
Dharmesh
Junaid and Peter have clearly, and in detail, given you all the techniques you require. These are the standard techniques.
From reading you original post your only problem appears to be how to stop a thread blocked in reading from a URL connection. Let T be the thread which uses the URL connection. Then what you want is that T should not remain blocked beyond a certain time. What you may do is this: just before opening/using URL connection, start a timer thread from T. Let the timer thread be TT. TT will wait (Thread.sleep(millis)) for a stipulated period of time and then check if T isAlive() and connection!=null. If true, then TT will call connection.close(). This will cause an IOException to be thrown, in thread T. Use this exception to make the thread return from the run() method. I think that should solve the problem. Junaid has already pointed to the solution and pointed out the API recommendations.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regarding Thread.stop()