Thread Contructor Question  
Author Message
rnurse





PostPosted: 2004-1-21 6:52:00 Top

java-programmer, Thread Contructor Question Hi All,

I'm curious about something. In a Java certification study guide,
they have the following code and they ask what the result is:

class MyThread extends Thread
{
public static void main(String [] args)
{
MyThread t = new MyThread();
Thread x = new Thread(t);
x.start();
}

public void run()
{
for (int i = 0; i < 3; ++i)
{
System.out.print(i + "..");
}
}
}

It's simple enough. Except, earlier in the text (and in the Java API)
they mention that the only constructors for the Thread class are:

Thread()
Thread(Runnable target)
Thread(Runnable target, String name)
Thread(String name)
Thread(ThreadGroup group, Runnable target)
Thread(ThreadGroup group, Runnable target, String name)
Thread(ThreadGroup group, Runnable target, String name, long
stackSize)
Thread(ThreadGroup group, String name)

I don't see Thread(Thread). And I don't see how it can be a Runnable
except for that they share the same method: run().
 
Anton Spaans





PostPosted: 2004-1-21 7:08:00 Top

java-programmer >> Thread Contructor Question "Robert Nurse" <email***@***.com> wrote in message
news:email***@***.com...
> Hi All,
>
> I'm curious about something. In a Java certification study guide,
> they have the following code and they ask what the result is:
>
> class MyThread extends Thread
> {
> public static void main(String [] args)
> {
> MyThread t = new MyThread();
> Thread x = new Thread(t);
> x.start();
> }
>
> public void run()
> {
> for (int i = 0; i < 3; ++i)
> {
> System.out.print(i + "..");
> }
> }
> }
>
> It's simple enough. Except, earlier in the text (and in the Java API)
> they mention that the only constructors for the Thread class are:
>
> Thread()
> Thread(Runnable target)
> Thread(Runnable target, String name)
> Thread(String name)
> Thread(ThreadGroup group, Runnable target)
> Thread(ThreadGroup group, Runnable target, String name)
> Thread(ThreadGroup group, Runnable target, String name, long
> stackSize)
> Thread(ThreadGroup group, String name)
>
> I don't see Thread(Thread). And I don't see how it can be a Runnable
> except for that they share the same method: run().


A Thread implements Runnable, so a Thread 'is-a' Runnable.
The "new Thread(t)" call treats t as a Runnable.

> I don't see Thread(Thread). And I don't see how it can be a Runnable
> except for that they share the same method: run()
Exactly.... Thread must implement run() as well because it must implement
Runnable.

-- Anton Spaans


 
Eric Sosman





PostPosted: 2004-1-21 7:16:00 Top

java-programmer >> Thread Contructor Question Robert Nurse wrote:
>
> Hi All,
>
> I'm curious about something. In a Java certification study guide,
> they have the following code and they ask what the result is:
>
> class MyThread extends Thread
> {
> public static void main(String [] args)
> {
> MyThread t = new MyThread();
> Thread x = new Thread(t);
> x.start();
> }
>
> public void run()
> {
> for (int i = 0; i < 3; ++i)
> {
> System.out.print(i + "..");
> }
> }
> }
>
> It's simple enough. Except, earlier in the text (and in the Java API)
> they mention that the only constructors for the Thread class are:
>
> Thread()
> Thread(Runnable target)
> Thread(Runnable target, String name)
> Thread(String name)
> Thread(ThreadGroup group, Runnable target)
> Thread(ThreadGroup group, Runnable target, String name)
> Thread(ThreadGroup group, Runnable target, String name, long
> stackSize)
> Thread(ThreadGroup group, String name)
>
> I don't see Thread(Thread). And I don't see how it can be a Runnable
> except for that they share the same method: run().

Thread implements Runnable, so MyThread (which extends
Thread) also implements Runnable. The constructors you're
using are Thread() (implicitly from the MyThread constructor)
and Thread(Runnable).

--
email***@***.com
 
 
Filip Larsen





PostPosted: 2004-1-21 7:45:00 Top

java-programmer >> Thread Contructor Question Robert Nurse wrote

> I don't see Thread(Thread). And I don't see how it can be a Runnable
> except for that they share the same method: run().

Thread implements Runnable.


Regards,
--
Filip Larsen


 
 
Tony Morris





PostPosted: 2004-1-21 8:54:00 Top

java-programmer >> Thread Contructor Question public class Thread implements Runnable
// java.lang.Thread provides a null implementation of the run() method.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


"Robert Nurse" <email***@***.com> wrote in message
news:email***@***.com...
> Hi All,
>
> I'm curious about something. In a Java certification study guide,
> they have the following code and they ask what the result is:
>
> class MyThread extends Thread
> {
> public static void main(String [] args)
> {
> MyThread t = new MyThread();
> Thread x = new Thread(t);
> x.start();
> }
>
> public void run()
> {
> for (int i = 0; i < 3; ++i)
> {
> System.out.print(i + "..");
> }
> }
> }
>
> It's simple enough. Except, earlier in the text (and in the Java API)
> they mention that the only constructors for the Thread class are:
>
> Thread()
> Thread(Runnable target)
> Thread(Runnable target, String name)
> Thread(String name)
> Thread(ThreadGroup group, Runnable target)
> Thread(ThreadGroup group, Runnable target, String name)
> Thread(ThreadGroup group, Runnable target, String name, long
> stackSize)
> Thread(ThreadGroup group, String name)
>
> I don't see Thread(Thread). And I don't see how it can be a Runnable
> except for that they share the same method: run().


 
 
lee





PostPosted: 2004-1-21 10:03:00 Top

java-programmer >> Thread Contructor Question In article <email***@***.com>, email***@***.com (Robert Nurse) wrote:
>Hi All,
>
>I'm curious about something. In a Java certification study guide,
>they have the following code and they ask what the result is:
>
>class MyThread extends Thread
>{
> public static void main(String [] args)
> {
> MyThread t = new MyThread();
> Thread x = new Thread(t);
> x.start();
> }
>
> public void run()
> {
> for (int i = 0; i < 3; ++i)
> {
> System.out.print(i + "..");
> }
> }
>}
>
>It's simple enough. Except, earlier in the text (and in the Java API)
>they mention that the only constructors for the Thread class are:
>
>Thread()
>Thread(Runnable target)
>Thread(Runnable target, String name)
>Thread(String name)
>Thread(ThreadGroup group, Runnable target)
>Thread(ThreadGroup group, Runnable target, String name)
>Thread(ThreadGroup group, Runnable target, String name, long
>stackSize)
>Thread(ThreadGroup group, String name)
>
>I don't see Thread(Thread). And I don't see how it can be a Runnable
>except for that they share the same method: run().

The Thread class implements the Runnable interface, so a Thread object (t)
qualifies as a Runnable target.

Lee Weiner
lee AT leeweiner DOT org
 
 
Knute Johnson





PostPosted: 2004-1-21 11:31:00 Top

java-programmer >> Thread Contructor Question Robert Nurse wrote:
> Hi All,
>
> I'm curious about something. In a Java certification study guide,
> they have the following code and they ask what the result is:
>
> class MyThread extends Thread
> {
> public static void main(String [] args)
> {
> MyThread t = new MyThread();
> Thread x = new Thread(t);
> x.start();
> }
>
> public void run()
> {
> for (int i = 0; i < 3; ++i)
> {
> System.out.print(i + "..");
> }
> }
> }
>
> It's simple enough. Except, earlier in the text (and in the Java API)
> they mention that the only constructors for the Thread class are:
>
> Thread()
> Thread(Runnable target)
> Thread(Runnable target, String name)
> Thread(String name)
> Thread(ThreadGroup group, Runnable target)
> Thread(ThreadGroup group, Runnable target, String name)
> Thread(ThreadGroup group, Runnable target, String name, long
> stackSize)
> Thread(ThreadGroup group, String name)
>
> I don't see Thread(Thread). And I don't see how it can be a Runnable
> except for that they share the same method: run().

It doesn't make any sense either. MyThread extends Thread and therefore
implements Runnable. So Thread x is redundant. It will work fine if
you just call the start() on t.

--

Knute Johnson
email s/nospam/knute/
Molon labe...

 
 
Tony Morris





PostPosted: 2004-1-21 12:05:00 Top

java-programmer >> Thread Contructor Question In an exam environment, that are more often that not, examples that are used
to assess competence in understanding of the language (especially the SCJP
exam) rather than practicality of the example used.

It is a fair exam question.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


"Knute Johnson" <email***@***.com> wrote in message
news:email***@***.com...
> Robert Nurse wrote:
> > Hi All,
> >
> > I'm curious about something. In a Java certification study guide,
> > they have the following code and they ask what the result is:
> >
> > class MyThread extends Thread
> > {
> > public static void main(String [] args)
> > {
> > MyThread t = new MyThread();
> > Thread x = new Thread(t);
> > x.start();
> > }
> >
> > public void run()
> > {
> > for (int i = 0; i < 3; ++i)
> > {
> > System.out.print(i + "..");
> > }
> > }
> > }
> >
> > It's simple enough. Except, earlier in the text (and in the Java API)
> > they mention that the only constructors for the Thread class are:
> >
> > Thread()
> > Thread(Runnable target)
> > Thread(Runnable target, String name)
> > Thread(String name)
> > Thread(ThreadGroup group, Runnable target)
> > Thread(ThreadGroup group, Runnable target, String name)
> > Thread(ThreadGroup group, Runnable target, String name, long
> > stackSize)
> > Thread(ThreadGroup group, String name)
> >
> > I don't see Thread(Thread). And I don't see how it can be a Runnable
> > except for that they share the same method: run().
>
> It doesn't make any sense either. MyThread extends Thread and therefore
> implements Runnable. So Thread x is redundant. It will work fine if
> you just call the start() on t.
>
> --
>
> Knute Johnson
> email s/nospam/knute/
> Molon labe...
>


 
 
Joseph Dionne





PostPosted: 2004-1-21 23:56:00 Top

java-programmer >> Thread Contructor Question Robert Nurse wrote:
> Hi All,
>
> I'm curious about something. In a Java certification study guide,
> they have the following code and they ask what the result is:
>
> class MyThread extends Thread
> {
> public static void main(String [] args)
> {
> MyThread t = new MyThread();
> Thread x = new Thread(t);
> x.start();
> }
>
> public void run()
> {
> for (int i = 0; i < 3; ++i)
> {
> System.out.print(i + "..");
> }
> }
> }
>
> It's simple enough. Except, earlier in the text (and in the Java API)
> they mention that the only constructors for the Thread class are:
>
> Thread()
> Thread(Runnable target)
> Thread(Runnable target, String name)
> Thread(String name)
> Thread(ThreadGroup group, Runnable target)
> Thread(ThreadGroup group, Runnable target, String name)
> Thread(ThreadGroup group, Runnable target, String name, long
> stackSize)
> Thread(ThreadGroup group, String name)
>
> I don't see Thread(Thread). And I don't see how it can be a Runnable
> except for that they share the same method: run().

the super() constructor is run during "MyThread t = new MyThread()"

 
 
Joseph Dionne





PostPosted: 2004-1-22 0:02:00 Top

java-programmer >> Thread Contructor Question Robert Nurse wrote:
> Hi All,
>
> I'm curious about something. In a Java certification study guide,
> they have the following code and they ask what the result is:
>
> class MyThread extends Thread
> {
> public static void main(String [] args)
> {
> MyThread t = new MyThread();
> Thread x = new Thread(t);
> x.start();
> }
>
> public void run()
> {
> for (int i = 0; i < 3; ++i)
> {
> System.out.print(i + "..");
> }
> }
> }
>
> It's simple enough. Except, earlier in the text (and in the Java API)
> they mention that the only constructors for the Thread class are:
>
> Thread()
> Thread(Runnable target)
> Thread(Runnable target, String name)
> Thread(String name)
> Thread(ThreadGroup group, Runnable target)
> Thread(ThreadGroup group, Runnable target, String name)
> Thread(ThreadGroup group, Runnable target, String name, long
> stackSize)
> Thread(ThreadGroup group, String name)
>
> I don't see Thread(Thread). And I don't see how it can be a Runnable
> except for that they share the same method: run().

Actually, you don't need "Thread x = new Thread(t)", the following works
just fine.

class MyThread extends Thread
{
public static void main(String [] args)
{
MyThread t = new MyThread();
t.start();
}

public void run()
{
for (int i = 0; i < 3; ++i)
{
System.out.print(i + "..");
}
}
}

 
 
BarryNL





PostPosted: 2004-1-22 0:12:00 Top

java-programmer >> Thread Contructor Question Joseph Dionne wrote:
> Robert Nurse wrote:
>
>> Hi All,
>>
>> I'm curious about something. In a Java certification study guide,
>> they have the following code and they ask what the result is:
>>
>> class MyThread extends Thread
>> {
>> public static void main(String [] args)
>> {
>> MyThread t = new MyThread();
>> Thread x = new Thread(t);
>> x.start();
>> }
>>
>> public void run()
>> {
>> for (int i = 0; i < 3; ++i)
>> {
>> System.out.print(i + "..");
>> }
>> }
>> }
>>
>> It's simple enough. Except, earlier in the text (and in the Java API)
>> they mention that the only constructors for the Thread class are:
>>
>> Thread()
>> Thread(Runnable target) Thread(Runnable target, String name)
>> Thread(String name)
>> Thread(ThreadGroup group, Runnable target) Thread(ThreadGroup group,
>> Runnable target, String name)
>> Thread(ThreadGroup group, Runnable target, String name, long
>> stackSize)
>> Thread(ThreadGroup group, String name)
>>
>> I don't see Thread(Thread). And I don't see how it can be a Runnable
>> except for that they share the same method: run().

java.lang.Thread implements java.lang.Runnable, thus the constructor
being used is Thread(Runnable target). In fact, having MyThread extends
Thread here serves no purpose - it may as well just implement Runnable.

>
> the super() constructor is run during "MyThread t = new MyThread()"
>
 
 
xarax





PostPosted: 2004-1-22 5:24:00 Top

java-programmer >> Thread Contructor Question "Joseph Dionne" <email***@***.com> wrote in message
news:KnxPb.37259$email***@***.com...
> Robert Nurse wrote:
> > Hi All,
> >
> > I'm curious about something. In a Java certification study guide,
> > they have the following code and they ask what the result is:
> >
> > class MyThread extends Thread
> > {
> > public static void main(String [] args)
> > {
> > MyThread t = new MyThread();
> > Thread x = new Thread(t);
> > x.start();
> > }
> >
> > public void run()
> > {
> > for (int i = 0; i < 3; ++i)
> > {
> > System.out.print(i + "..");
> > }
> > }
> > }
> >
> > It's simple enough. Except, earlier in the text (and in the Java API)
> > they mention that the only constructors for the Thread class are:
> >
> > Thread()
> > Thread(Runnable target)
> > Thread(Runnable target, String name)
> > Thread(String name)
> > Thread(ThreadGroup group, Runnable target)
> > Thread(ThreadGroup group, Runnable target, String name)
> > Thread(ThreadGroup group, Runnable target, String name, long
> > stackSize)
> > Thread(ThreadGroup group, String name)
> >
> > I don't see Thread(Thread). And I don't see how it can be a Runnable
> > except for that they share the same method: run().
>
> Actually, you don't need "Thread x = new Thread(t)", the following works
> just fine.
/snip better code example/

But that wasn't the point of the test question. The
point was to determine whether the testee knew that
the Thread class implements Runnable.