Solved

Runtime.addShutDownHook() prefers Thread above Runnable

Posted on 2003-11-10
17
925 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
Independent Software Vendors: 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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…

734 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