| Author |
passing an instance of Thread rather than Runnable to new Thread instance
|
Jasper Vader
Ranch Hand
Joined: Jan 10, 2003
Posts: 284
|
|
a rather long-winded title! here is the question from JDiscuss that has prompted this question... Given the following class: class Counter { public int startHere = 1; public int endHere = 100; public static void main(String[] args) { new Counter().go(); } void go() { // A Thread t = new Thread(a); t.start(); } } What block of code can you place at line A above so that this program will count from startHere to endHere? A. Runnable a = new Runnable() { public void run() { for (int i = startHere; i <= endHere; i++){ System.out.println(i); } } }; B. a implements Runnable { public void run() { for (int i = startHere; i <= endHere; i++){ System.out.println(i); } } }; C. Thread a = new Thread() { public void run() { for (int i = startHere; i <= endHere; i++){ System.out.println(i); } } }; ANSWERS A B C Only A. Only C. the answer is A and C. ps- in each example, are they trying to create an anonymous implementing (of Runnable) or subclass (of Thread) class by putting the curly braces in before the semicolon? is it an argument-local inner class?
|
giddee up
|
 |
Pallavi Chakraborty
Ranch Hand
Joined: Jan 18, 2003
Posts: 93
|
|
Hi Jasper, Can you explain which semicolon you are refering to. I think where they say new Runnable that is an Anonymous implementation. Thank you very much Pallavi
|
 |
Garrett Smith
Ranch Hand
Joined: Jun 27, 2002
Posts: 401
|
|
Thread implements Runnable, so when you pass a Thread to a Thread constructor, it is being widened to its interface type, Runnable. http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25215
5.1.4 Widening Reference Conversions The following conversions are called the widening reference conversions: From any class type S to any class type T, provided that S is a subclass of T. (An important special case is that there is a widening conversion to the class type Object from any other class type.) From any class type S to any interface type K, provided that S implements K. From the null type to any class type, interface type, or array type. From any interface type J to any interface type K, provided that J is a subinterface of K. From any interface type to type Object. From any array type to type Object. From any array type to type Cloneable. From any array type to type java.io.Serializable From any array type SC[] to any array type TC[], provided that SC and TC are reference types and there is a widening conversion from SC to TC. Such conversions never require a special action at run time and therefore never throw an exception at run time. They consist simply in regarding a reference as having some other type in a manner that can be proved correct at compile time.
|
comp.lang.javascript FAQ: http://jibbering.com/faq/
|
 |
Sarma Lolla
Ranch Hand
Joined: Oct 21, 2002
Posts: 203
|
|
A. Runnable a = new Runnable() { public void run() { for (int i = startHere; i <= endHere; i++){ System.out.println(i); } } }; B. a implements Runnable { public void run() { for (int i = startHere; i <= endHere; i++){ System.out.println(i); } } }; C. Thread a = new Thread() { public void run() { for (int i = startHere; i <= endHere; i++){ System.out.println(i); } } };
Answer B is wrong as annonymous inner class can't explicitely use the word implements or extends. There is no problem with both A and C. In these type of questions you need to see if the annonymous class is using any non final local variables of the method. Luckly in this example all the variables are in member variables and that makes answering this question very simple and straight forward. Where ever you can send Runnable you can also send Thread as Thread is already implementing Runnable. Thats it.
|
 |
Jasper Vader
Ranch Hand
Joined: Jan 10, 2003
Posts: 284
|
|
|
thanks all for the help. Garret - you really have taken a shine to the sun docs. a good thing, i should get back into reading them (just been reading the kathy_bert book exclusively recently).
|
 |
Garrett Smith
Ranch Hand
Joined: Jun 27, 2002
Posts: 401
|
|
Garret - you really have taken a shine to the sun docs
I'm studying for dcjp (Dan Certified Java Programmer).
|
 |
 |
|
|
subject: passing an instance of Thread rather than Runnable to new Thread instance
|
|
|