Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Programmer Certification (OCPJP)
Runnable to Thread Casting
Balaji Bang
Ranch Hand
Posts: 182
I like...
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
public class AnoThreads{ static volatile java.io.PrintStream s=System.out; public static void main(String args[]){ Runnable r1=new Runnable(){ public void run(){ synchronized(s){ System.out.print("SCJP-1 "); System.out.print("SCJP-2 "); System.out.print("SCJP-3 "); } } }; Runnable r2=new Runnable(){ public void run(){ synchronized(s){ System.out.print("SCEA-1 "); System.out.print("SCEA-2 "); System.out.print("SCEA-3 "); } } }; //r1.run(); //r2.run(); //Thread t1=new Thread(r1); //Thread t2=new Thread(r2); Thread t1=(Thread)r1; //ClassCastException Thread t2=(Thread)r2;//ClassCastException t1.start(); t2.start(); } }
Why the casting gives ClassCastException?
Thread
class provides the implementation for Runnable interface. Then why cannot we cast???
Abhi vijay
Ranch Hand
Posts: 509
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
This is because r1 is an implementer of the Runnable Interface. Thread class also is an implementer of Runnable. But there is no relation between r1(anonymous sub-class referenced by r1) and the Thread class.
Balaji Bang
Ranch Hand
Posts: 182
I like...
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
ohhhhhh. Yes r1 is just an implementor reference not Runnable reference. So we cannot . Thank you
A timing clock, fuse wire, high explosives and a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Threads blocking on static method.... or do they?
Synchronization lock
Thread Synchronization
Exercise 13.6 in - A Programmer's Guide to Java SCJP Certification - 3rd Ed
threads and uncaught exceptions
More...