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


Groups > comp.lang.java.programmer > #29258 > unrolled thread

canonical thread stop

Started bymike75214@usa.com
First post2014-07-23 23:09 -0700
Last post2014-07-24 09:00 -0400
Articles 3 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  canonical thread stop mike75214@usa.com - 2014-07-23 23:09 -0700
    Re: canonical thread stop markspace <markspace@nospam.nospam> - 2014-07-23 23:22 -0700
    Re: canonical thread stop Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-07-24 09:00 -0400

#29258 — canonical thread stop

Frommike75214@usa.com
Date2014-07-23 23:09 -0700
Subjectcanonical thread stop
Message-ID<7cfe4089-8acd-4523-8ff7-3bad175571c3@googlegroups.com>
Is there a canonical way to stop a thread in Java?  (i.e. a method provided by Oracle)

[toc] | [next] | [standalone]


#29259

Frommarkspace <markspace@nospam.nospam>
Date2014-07-23 23:22 -0700
Message-ID<lqq8mn$vo8$1@dont-email.me>
In reply to#29258
On 7/23/2014 11:09 PM, mike75214@usa.com wrote:
> Is there a canonical way to stop a thread in Java?  (i.e. a method
> provided by Oracle)
>

Thread.stop()

[toc] | [prev] | [next] | [standalone]


#29270

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2014-07-24 09:00 -0400
Message-ID<lqr01m$jin$1@dont-email.me>
In reply to#29258
On 7/24/2014 2:09 AM, mike75214@usa.com wrote:
> Is there a canonical way to stop a thread in Java?  (i.e. a method provided by Oracle)

     System.exit(status);  // tee-hee

     That's only partly in jest.  If you stop (or suspend) a thread "from
the outside" without its cooperation, you have absolutely no idea what
its state was.  For example, it might have just stored the X position of
something and have been about to store the Y position when the rug was
ripped from beneath its feet; any other thread that subsequently uses
the combined (X,Y) will get a completely bogus location -- the X from a
moment ago and the Y from last week.

     The only safe way to stop or suspend a thread is with the thread's
cooperation.  It needs to get to a "safe" state, it needs to "put its
affairs in order" before going to sleep or to the grave.  And since
what constitutes "safe" and "in order" is application-dependent, there's
no one-size-fits-all pre-canned solution.

     Here's an analogy: You're driving down the highway when you hear a
siren and see flashing blue lights in your rear-view mirror.  So you
pull over to the side of the road, slow down, stop, and await your
fate.  That's the right way to stop a thread: Signal it to stop and
wait for it to do so safely.

     If instead the cop had zapped you with a phaser set on "stun,"
you'd find yourself hurtling down the road at high speed, paralyzed
and unable to operate the controls, a menace to yourself and to
others.  That's the wrong way to stop a thread: Just halt it willy-
nilly and pray there won't be too much "collateral damage."

-- 
esosman@comcast-dot-net.invalid

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web