Solved

Runtime.addShutDownHook() prefers Thread above Runnable

Posted on 2003-11-10
17
917 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why my table column Id is not passed to java object? 4 46
hibernate example using maven 12 77
Java basic valueOf question 1 33
Java Eclipse Loop 3 29
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

762 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