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


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

Re: canonical thread stop

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: canonical thread stop
Date 2014-07-24 09:00 -0400
Organization A noiseless patient Spider
Message-ID <lqr01m$jin$1@dont-email.me> (permalink)
References <7cfe4089-8acd-4523-8ff7-3bad175571c3@googlegroups.com>

Show all headers | View raw


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

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


Thread

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

csiph-web