Solved

Runtime.addShutDownHook() prefers Thread above Runnable

Posted on 2003-11-10
17
928 Views
Last Modified: 2012-06-21
///////////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.
0
Comment
Question by:sciuriware
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
  • 4
17 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 9713265
 What's the error message?
0
 
LVL 24

Author Comment

by:sciuriware
ID: 9713327
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
 
LVL 15

Expert Comment

by:jimmack
ID: 9713380
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 35

Expert Comment

by:girionis
ID: 9713410
 Weird... Can you try and cast X to Thread?
0
 
LVL 24

Author Comment

by:sciuriware
ID: 9713461
No, can't cast.
;JOOP!
0
 
LVL 35

Expert Comment

by:girionis
ID: 9713465
 If all else fails use an inner thread class.
0
 
LVL 35

Expert Comment

by:girionis
ID: 9713514
 Can't you do:

Runnable x = new X();
Thread t = (Thread) x;
Runtime.getRuntime().addShutdownHook(x);
0
 
LVL 35

Expert Comment

by:girionis
ID: 9713522
 Ehm.. sorry I meant:

Runnable x = new X();
Runtime.getRuntime().addShutdownHook((Thread) x);
0
 
LVL 24

Author Comment

by:sciuriware
ID: 9713551
No girionis, you can't cast a Runnable to a Thread.
Did you check your advises in advance?
;JOOP!
0
 
LVL 35

Expert Comment

by:girionis
ID: 9713651
 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
 
LVL 15

Accepted Solution

by:
jimmack earned 50 total points
ID: 9713652
>> 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
 
LVL 24

Author Comment

by:sciuriware
ID: 9713741
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
 
LVL 15

Expert Comment

by:jimmack
ID: 9713763
;-)

Thanx.
0
 
LVL 35

Expert Comment

by:girionis
ID: 9713821
 Actually this:

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

  *compiles perfectly* even though it throws a run-time error. ;-)
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9713843
That's because the compiler assumes (in this case, falsely ;-)) that if you're forcing a cast, you know what you're doing ;-)
0
 
LVL 24

Author Comment

by:sciuriware
ID: 9713850
Thank you girionis, although you can't make me happy with runtime errors .......
;JOOP!
0
 
LVL 35

Expert Comment

by:girionis
ID: 9713875
 jimmack true, I just thought sciuriware meant a compile-time error.
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question