Solved

Runtime.addShutDownHook() prefers Thread above Runnable

Posted on 2003-11-10
17
888 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
  • 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now