Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Runtime.addShutDownHook() prefers Thread above Runnable

Posted on 2003-11-10
17
Medium Priority
?
940 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 150 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

636 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