When the stop() in Thread class changed to final method?

R

Rogan Dawes

RC said:
If you go to this link

http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html


It tell you can override the stop() method in the java.lang.Thread class

But I got compiled error

.\MyThread.java:22: stop() in MyThread cannot override stop() in
java.lang.Thread; overridden method is final
public void stop() {
^
1 error

My compiler is 1.5.0_10

Why is that?


Actually, I don't see anywhere in the referenced document that suggests
that you can override Thread.stop().

There are a number of examples where classes have a stop() method, but
these are not sublasses of Thread that I can see.

Besides, that document is being very clear about why these methods are
deprecated. Why on earth would you want to override them?

If you have code that is calling Thread.stop() that you wish to fix, it
would be a much better idea to fix the calling code instead.

Rogan
 
D

Daniel Pitts

If you go to this link

http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDepreca...

It tell you can override the stop() method in the java.lang.Thread class

But I got compiled error

.\MyThread.java:22: stop() in MyThread cannot override stop() in
java.lang.Thread; overridden method is final
public void stop() {
^
1 error

My compiler is 1.5.0_10

Why is that?'

That document you linked to is suggesting that you override the stop
method in Applet, not Thread.
 
R

RC

That document you linked to is suggesting that you override the stop
method in Applet, not Thread.

Then, do you know how I can stop a thread since Thread.stop()
has deprecated. Can I just make a thread = null?
 
D

Daniel Pitts

Then, do you know how I can stop a thread since Thread.stop()
has deprecated. Can I just make a thread = null?

No, you have to tell the thread somehow that it should stop
gracefully, or interrupt it.

public class MyThread extends Thread {
private volatile boolean active;
public MyThread() {
active = true;
}
public void run() {
try {
while (active) {
// doThings();
}
} catch (InterruptedException e) {
}
}
public void gracefullyQuit() {
active = false;
}
}


hope this helps,
Daniel.
 
E

Eric Sosman

RC wrote On 02/01/07 16:58,:
Then, do you know how I can stop a thread since Thread.stop()
has deprecated. Can I just make a thread = null?

The reason Thread.stop() is deprecated isn't because of
some deficiency in the method, but because the entire idea
of stopping a Thread dead in its tracks is unsafe. Perhaps
you were taught as a child that putting your hand on a hot
stove is deprecated; it does not follow that putting your
fanny there would be an improvement.

Here's an analogy that may help you understand why it
is a bad idea to stop a Thread without its cooperation. As
a wealthy person, you no doubt have lots of investments and
a personal banker to take care of them. The banker is busy
on your behalf, moving money from one account to another all
day long, trying to make your Midas-like wealth accumulate
as fast as possible. Your banker, if you haven't already
guessed, is named Mr. Thread.

One day, in a fit of rich man's pique, you decide you
no longer need the banker's services, so you say to Thread
"Stop right where you are. You're fired. Out of my sight,
and stay out!"

Unfortunately for you, Thread had just that moment taken
a bazillion dollars out of one of your accounts and was about
to deposit it in another. But you told him to stop, so that's
what he did -- and he left your sight, as instructed, still
with your bazillion dollars in his hands. Welcome to the
poorhouse, Mister Rich Man!

Do you get the point now? If you stop a Thread at some
random point in its execution, you don't know what it was
doing at the moment it ceased to execute. It might have been
somewhere in the middle of a sequence of steps that should
either be completed in their entirety or not begun at all;
the half-completed states are not desirable.

So, how do you stop a Thread in a controlled fashion?
One way is to have the Thread's Runnable check a "please shut
down now" flag every so often, at a point where things are
stable and there's no half-completed sequence of steps in
progress. Instead of firing your banker on the instant, you
say at the beginning of your relationship, "Thread, I want
you to call my office precisely at nine o'clock every morning,
before starting work, in case I have special instructions."
And when the day comes, your instructions are "Clear out your
desk, Thread." That way, he's fired when he's not in the
middle of something you'd prefer weren't interrupted.
 
R

Rogan Dawes

RC said:
Then, do you know how I can stop a thread since Thread.stop()
has deprecated. Can I just make a thread = null?

Did you not read the link that YOU provided above?

It has all the advice and examples you need to answer that question.

Rogan
 
N

nukleus

Stopping a thread is just the worst possible thing
you can imagine. In many cases, you'll simply crash you
entire app if you don't catch that exception in
the right place.

Thread should be stopped by exiting it main run()
loop. The simpliest thing is to set some variable
that is tested in the run loop. That variable could
be set by just about any method or a result of
some user action, such as "cancel" or from some
event handler code.

But when you simply slaugher thread with stop(),
there may be all sorts of things happening
in your app, things may be locked, some other
threads may be waiting for some operation to complete,
and so on. Because once you even utter the word
"thread", it implies theare are other threads in
your app, and they all should coordinate the
access to common resources, such as files, sockets,
gui elements or you name it.

So...

What you are creating by doing that stop() thing,
is making your entire app inconsistent, when one
part thinks that it has a valid access to some
variable or resource, but the other part,
sitting on the same resource is dead.

You'll never make your program stable.
That is for sure.

Just about all you can achive with it
is an ugly hack.

My suggestion is to completely forget this idea,
because exiting thread properly is about the simpliest
thing in the world, at least in most cases.

And make sure that if that thread is associated
with some frame in your app, you first exit thread,
and then make sure to close that frame by doing
dispose().

This way, your entire program's logic
will operate as intuitively expected by the user.

Good luck.
 
L

Lew

nukleus said:
Stopping a thread is just the worst possible thing
you can imagine. In many cases, you'll simply crash you
entire app if you don't catch that exception in
the right place.

Thread should be stopped by exiting it main run()
loop. The simpliest thing is to set some variable
that is tested in the run loop. That variable could
be set by just about any method or a result of
some user action, such as "cancel" or from some
event handler code.

This is the good advice. Nukleus is right on the mark with this.

- Lew
 
R

Rogan Dawes

Lew said:
This is the good advice. Nukleus is right on the mark with this.

- Lew

As is the URL that the original poster provided, and that I suggested he
go back and read ;-)

Rogan
 
N

nukleus

Forgot to tellya.

First of all, you can't possibly set thread to null.
Whatever you set in your program,
is just your copy of a pointer to a thread.
The thread exists in JVM or operating system
and it could care less what you set in your program.
Just about the only thing you can do to it,
is to ethier exit gracefuly,
or slaughter it, which will simply crash your program,
at least in some cases.

Making your thread variable null
does not affect that tread in ANY way.
It only takes away your ability to refer to it
from your program.

Anotherwords, it doesn't do a jack shit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,269
Messages
2,571,099
Members
48,773
Latest member
Kaybee

Latest Threads

Top