Thread Contructor Question

R

Robert Nurse

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

Anton Spaans

Robert Nurse said:
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
 
E

Eric Sosman

Robert said:
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).
 
F

Filip Larsen

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,
 
T

Tony Morris

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)
 
L

Lee Weiner

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
 
K

Knute Johnson

Robert said:
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.
 
T

Tony Morris

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 said:
Robert said:
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...
 
J

Joseph Dionne

Robert said:
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()"
 
J

Joseph Dionne

Robert said:
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 + "..");
}
}
}
 
B

BarryNL

Joseph said:
Robert said:
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.
 
X

xarax

Joseph Dionne said:
Robert said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top