A
anthony.jayasekera
Hi,
Like many before me I have recently been wrestling with the fact that
Thread.stop() has been deprecated and as such, if I want to halt a
thread during some processing that does not periodically check whether
to keep going or have the potential to throw InterruptedException's, I
basically have to go fly a kite! I have read the reason why
Thread.stop() is bad, namely that it unlocks any monitors associated
with the thread (I am not aware of any other problems). However I
believe that in my particular scenario this may not be an issue. Here
is my scenario:
I am trying to implement a cancellable, modal WaitDialog for display
whilst executing long running code. In my specific usage of this dialog
the long running code is wrapped up in a single third party api call
over which I have no control. It does not throw InterruptedException's
and can take a very long time to execute.
The only monitor I explicitly assign is one which locks access to
disposal of the dialog. My worker thread executes the long running code
and once completed disposes of the dialog in a locked section of code.
Importantly, this code checks to see if the dialog is disposed (using
isDisplayable()) before disposing it. The click handler for the cancel
button on the WaitDialog also performs this checked dispose.
control leaves the calling thread at this point. When control returns
to the thread (i.e. when the dialog has been disposed) I check to see
if the worker is still running (using isAlive()) and if so, ......, I
call Thread.stop()!!! The isAlive check and stop call are also done in
a block synchronised on the same monitor as the dispose code.
So, when I call Thread.stop() the only lock affected is the one
sychronising the the disposal. If it is unlocked and the worker thread
is currently waiting on it the waiting synchronised code won't execute
since Thread.stop() can only be called after the dialog has been
disposed (remember it checks to see if the dialog is disposed before
executing).
Therefore, as far as I can see there will be no adverse affects of
calling Thread.stop() in this situation. Of course if there are any
implicit monitors that my application code doesn't know about that
would be a different matter. Is there anything like this?
Of course I haven't even approached the possibility that the deprecated
stop method may be dropped entirely but if this scenario is a valid one
perhaps the method should not be dropped?
Any opinions about this would be gladly received.
Thanks,
Anthony
Like many before me I have recently been wrestling with the fact that
Thread.stop() has been deprecated and as such, if I want to halt a
thread during some processing that does not periodically check whether
to keep going or have the potential to throw InterruptedException's, I
basically have to go fly a kite! I have read the reason why
Thread.stop() is bad, namely that it unlocks any monitors associated
with the thread (I am not aware of any other problems). However I
believe that in my particular scenario this may not be an issue. Here
is my scenario:
I am trying to implement a cancellable, modal WaitDialog for display
whilst executing long running code. In my specific usage of this dialog
the long running code is wrapped up in a single third party api call
over which I have no control. It does not throw InterruptedException's
and can take a very long time to execute.
The only monitor I explicitly assign is one which locks access to
disposal of the dialog. My worker thread executes the long running code
and once completed disposes of the dialog in a locked section of code.
Importantly, this code checks to see if the dialog is disposed (using
isDisplayable()) before disposing it. The click handler for the cancel
button on the WaitDialog also performs this checked dispose.
WaitDialog (using Dialog.setVisible()). Since the dialog is modalFrom my calling thread I start the worker thread and display the
control leaves the calling thread at this point. When control returns
to the thread (i.e. when the dialog has been disposed) I check to see
if the worker is still running (using isAlive()) and if so, ......, I
call Thread.stop()!!! The isAlive check and stop call are also done in
a block synchronised on the same monitor as the dispose code.
So, when I call Thread.stop() the only lock affected is the one
sychronising the the disposal. If it is unlocked and the worker thread
is currently waiting on it the waiting synchronised code won't execute
since Thread.stop() can only be called after the dialog has been
disposed (remember it checks to see if the dialog is disposed before
executing).
Therefore, as far as I can see there will be no adverse affects of
calling Thread.stop() in this situation. Of course if there are any
implicit monitors that my application code doesn't know about that
would be a different matter. Is there anything like this?
Of course I haven't even approached the possibility that the deprecated
stop method may be dropped entirely but if this scenario is a valid one
perhaps the method should not be dropped?
Any opinions about this would be gladly received.
Thanks,
Anthony