Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #24866

Re: What is so bad aboud Thread.stop() ?

From Kevin McMurtrie <mcmurtrie@pixelmemory.us>
Newsgroups comp.lang.java.programmer
Subject Re: What is so bad aboud Thread.stop() ?
References <8fe57dc2-a323-4821-941c-4036663cbce5@googlegroups.com>
Date 2013-07-31 21:09 -0700
Message-ID <51f9df70$0$52766$742ec2ed@news.sonic.net> (permalink)
Organization Sonic.Net

Show all headers | View raw


In article <8fe57dc2-a323-4821-941c-4036663cbce5@googlegroups.com>,
 taqmcg@gmail.com wrote:

> The use of Thread.stop() is strongly deprecated, but I'm a bit confused by 
> the explanation given in the documentation: (e.g., 
> http://docs.oracle.com/javase/6/docs/technotes/guides/concurrency/threadPrimit
> iveDeprecation.html)
> 
>   Because it is inherently unsafe. Stopping a thread causes 
>   it to unlock all the monitors that it has locked. (The 
>   monitors are unlocked as the ThreadDeath exception propagates 
>   up the stack.) If any of the objects previously protected by
>   these monitors were in an inconsistent state, other threads
>   may now view these objects in an inconsistent state.
>   Such objects are said to be damaged. When threads operate on
>   damaged objects, arbitrary behavior can result. This behavior 
>   may be subtle and difficult to detect, or it may be pronounced. 
>   Unlike other unchecked exceptions, ThreadDeath kills threads silently;
>   thus, the user has no warning that his program may be corrupted.
>   The corruption can manifest itself at any time after the actual 
>   damage occurs, even hours or days in the future.
> 
> As I understand this, the ThreadDeath exception is treated exactly as any 
> other exception -- finally blocks are honored, it can even be caught -- that 
> might be thrown unexpectedly.  E.g., if we got an OutOfMemoryError or we 
> happen to  explicitly throw a RuntimeException we're going to see exactly the 
> same behavior with monitors released progressively as we go up the stack.  
> It's certainly easy enough  to catch and rethrow the ThreadDeath in its run 
> method
> if I really want to log it and address the one difference.
> 
> So what is the big hullaballoo about?  Is there something special going on 
> here with Thread.stop(), or is this just a specific instance of the fact the 
> we should be very careful whenever a thread has been stopped by an unexpected 
> exception?
> 
> Thread.stop() might be called at any time in the to-be-stopped thread, and 
> that might be an issue, but at least in principle the thread that issues the 
> stop could check the to-be-stopped thread and only stop it at 'appropriate' 
> points where appropriate is defined by the user.  
> 
> E.g., suppose I have some concern that a piece of code that I'm about to run 
> might have an infinite loop in it (say I'm testing somebody else's regular 
> expression parser with lots of different RE's), but it's not going to 
> allocate any external resources (e.g., files, DB connections).  I could 
> set/clear a flag as I enter and leave the suspect code.  Could I safely 
> monitor this thread in another thread.  After say 100 seconds if a given 
> expression hasn't parsed can the monitor thread invoke a Thread.stop() safely 
> if my flag shows that the RE thread is caught in the parser?
> 
> Thanks for your insight.
>      Regards,
>      Tom MCglynn

The problem is that it causes an exception from code that can not throw 
an exception.  It corrupts the data in shared objects.  Example:

if (thing == null)
   throw new NullPointerException("thing must not be null");
data[++count]= thing;

This could be killed after incrementing 'count' but before assigning the 
array element.  Now you have a very illegal null element.
-- 
I will not see posts from Google because I must filter them as spam

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

What is so bad aboud Thread.stop() ? taqmcg@gmail.com - 2013-07-31 06:36 -0700
  Re: What is so bad aboud Thread.stop() ? Marcel Müller <news.5.maazl@spamgourmet.org> - 2013-07-31 18:31 +0200
  Re: What is so bad aboud Thread.stop() ? Arne Vajhøj <arne@vajhoej.dk> - 2013-07-31 12:35 -0400
    Re: What is so bad aboud Thread.stop() ? Lew <lewbloch@gmail.com> - 2013-07-31 14:13 -0700
      Re: What is so bad aboud Thread.stop() ? usvirtualobservatory@gmail.com - 2013-08-01 07:38 -0700
        Re: What is so bad aboud Thread.stop() ? Lew <lewbloch@gmail.com> - 2013-08-01 14:07 -0700
          Re: What is so bad aboud Thread.stop() ? Steven Simpson <ss@domain.invalid> - 2013-08-01 23:15 +0100
            Re: What is so bad aboud Thread.stop() ? taqmcg@gmail.com - 2013-08-01 16:16 -0700
              Re: What is so bad aboud Thread.stop() ? "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2013-08-02 18:35 +0100
                Re: What is so bad aboud Thread.stop() ? Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2013-08-03 19:21 -0700
              Re: What is so bad aboud Thread.stop() ? Arne Vajhøj <arne@vajhoej.dk> - 2013-08-02 21:58 -0400
                Re: What is so bad aboud Thread.stop() ? taqmcg@gmail.com - 2013-08-03 05:08 -0700
  Re: What is so bad aboud Thread.stop() ? Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2013-07-31 21:09 -0700
    Re: What is so bad aboud Thread.stop() ? usvirtualobservatory@gmail.com - 2013-08-01 08:00 -0700
      Re: What is so bad aboud Thread.stop() ? Arne Vajhøj <arne@vajhoej.dk> - 2013-08-01 11:25 -0400
        Re: What is so bad aboud Thread.stop() ? Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-08-01 11:49 -0400
          Re: What is so bad aboud Thread.stop() ? Arne Vajhøj <arne@vajhoej.dk> - 2013-08-02 21:51 -0400
        Re: What is so bad aboud Thread.stop() ? taqmcg@gmail.com - 2013-08-01 09:30 -0700
          Re: What is so bad aboud Thread.stop() ? markspace <markspace@nospam.nospam> - 2013-08-01 10:01 -0700
          Re: What is so bad aboud Thread.stop() ? Arne Vajhøj <arne@vajhoej.dk> - 2013-08-02 21:55 -0400
  Re: What is so bad aboud Thread.stop() ? Sven Köhler <remove-sven.koehler@gmail.com> - 2013-08-01 18:28 +0300
  Re: What is so bad aboud Thread.stop() ? "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2013-08-02 18:50 +0100
    Re: What is so bad aboud Thread.stop() ? taqmcg@gmail.com - 2013-08-02 11:34 -0700
      Re: What is so bad aboud Thread.stop() ? Arne Vajhøj <arne@vajhoej.dk> - 2013-08-02 22:04 -0400
        Re: What is so bad aboud Thread.stop() ? "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2013-08-03 10:10 +0100
      Re: What is so bad aboud Thread.stop() ? "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2013-08-03 10:28 +0100
        Re: What is so bad aboud Thread.stop() ? taqmcg@gmail.com - 2013-08-03 05:00 -0700
          Re: What is so bad aboud Thread.stop() ? blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2013-08-11 17:46 +0000
            Re: What is so bad aboud Thread.stop() ? taqmcg@gmail.com - 2013-08-13 11:47 -0700
              Re: What is so bad aboud Thread.stop() ? blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2013-08-13 20:18 +0000
                Re: What is so bad aboud Thread.stop() ? Patricia Shanahan <pats@acm.org> - 2013-08-13 13:51 -0700
                Re: What is so bad aboud Thread.stop() ? blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2013-08-14 22:42 +0000
                Re: What is so bad aboud Thread.stop() ? taqmcg@gmail.com - 2013-08-13 14:10 -0700
                Re: What is so bad aboud Thread.stop() ? Lew <lewbloch@gmail.com> - 2013-08-13 15:22 -0700
                Re: What is so bad aboud Thread.stop() ? Arivald <NOSPAMarivald@interia.pl> - 2013-08-14 01:08 +0200
                Re: What is so bad aboud Thread.stop() ? markspace <markspace@nospam.nospam> - 2013-08-13 16:45 -0700
                Re: What is so bad aboud Thread.stop() ? Arivald <NOSPAMarivald@interia.pl> - 2013-08-14 08:42 +0200
                Re: What is so bad aboud Thread.stop() ? Marcel Müller <news.5.maazl@spamgourmet.org> - 2013-08-14 09:48 +0200
                Re: What is so bad aboud Thread.stop() ? taqmcg@gmail.com - 2013-08-14 08:55 -0700
                Re: What is so bad aboud Thread.stop() ? markspace <markspace@nospam.nospam> - 2013-08-14 19:10 -0700
                Re: What is so bad aboud Thread.stop() ? taqmcg@gmail.com - 2013-08-15 06:51 -0700
                Re: What is so bad aboud Thread.stop() ? Lew <lewbloch@gmail.com> - 2013-08-15 17:35 -0700
                Re: What is so bad aboud Thread.stop() ? taqmcg@gmail.com - 2013-08-16 06:30 -0700
                Re: What is so bad aboud Thread.stop() ? blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2013-08-14 22:43 +0000
                Re: What is so bad aboud Thread.stop() ? taqmcg@gmail.com - 2013-08-15 12:12 -0700

csiph-web