Runtime.addShutDownHook() prefers Thread above Runnable

///////////I was puzzled when my code was refused by the compiler:
public class X implements Runnable
{
    public void run() { ..... }    // What ever when it dies.
    private X(){}                    //  Don't create objects.
    public static void action()  // One of the methods, not instantiating this class.
    {
         Runtime.getRuntime().addShutdownHook(new X());  // Last will ....
    }
}
///////Things got better with :
public class X extends Thread
....
//////I should understand this but the coin won't drop this morning.
LVL 24
sciuriwareAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

girionisCommented:
 What's the error message?
0
sciuriwareAuthor Commented:
Cut from the ECLIPSE error window:
Error
  The method addShutdownHook(Thread) in the type Runtime is not applicable for the arguments (X)      X.java      GoiDo/RiengTu      line 125

Don't be confused by the package name.
;JOOP!
0
jimmackCommented:
It seems that addShutdownHook requires a Thread as a parameter.  It must need some of the facilities provided by a Thread, otherwise the parameter would be a Runnable (as you originally tried).

The method can't down-cast your Runnable to a Thread.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

girionisCommented:
 Weird... Can you try and cast X to Thread?
0
sciuriwareAuthor Commented:
No, can't cast.
;JOOP!
0
girionisCommented:
 If all else fails use an inner thread class.
0
girionisCommented:
 Can't you do:

Runnable x = new X();
Thread t = (Thread) x;
Runtime.getRuntime().addShutdownHook(x);
0
girionisCommented:
 Ehm.. sorry I meant:

Runnable x = new X();
Runtime.getRuntime().addShutdownHook((Thread) x);
0
sciuriwareAuthor Commented:
No girionis, you can't cast a Runnable to a Thread.
Did you check your advises in advance?
;JOOP!
0
girionisCommented:
 I didn't actually. I thought the compiler would accept it (I should go back to reading simple OO concepts I guess :( )... anyway just use an inner Thread class:

Runtime.getRuntime().addShutdownHook(new Thread() {...});
0
jimmackCommented:
>> you can't cast a Runnable to a Thread

That's the problem and explains why you can't pass the Runnable to the addShutdownHook.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sciuriwareAuthor Commented:
As I prefer to extend my classes or to implement on them (access to variables) I accept that I must extend from Thread.
Although the answer did not explain everything I guess jimmack kept to the rules and facts of JAVA. And 50 points is not much in these days ....
;JOOP!
0
jimmackCommented:
;-)

Thanx.
0
girionisCommented:
 Actually this:

>Runnable x = new X();
>Runtime.getRuntime().addShutdownHook((Thread) x);

  *compiles perfectly* even though it throws a run-time error. ;-)
0
jimmackCommented:
That's because the compiler assumes (in this case, falsely ;-)) that if you're forcing a cast, you know what you're doing ;-)
0
sciuriwareAuthor Commented:
Thank you girionis, although you can't make me happy with runtime errors .......
;JOOP!
0
girionisCommented:
 jimmack true, I just thought sciuriware meant a compile-time error.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.