• 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

passing an instance of Thread rather than Runnable to new Thread instance

 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

     
    Ranch Hand
    Posts: 203
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    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
    Posts: 284
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 401
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Garret - you really have taken a shine to the sun docs


    I'm studying for dcjp (Dan Certified Java Programmer).
     
    Hoo hoo hoo! Looks like we got a live one! Here, wave this tiny ad at it:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic