File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

Sangita Mishra
Greenhorn

Joined: Jun 15, 2005
Posts: 22
Can a class extend Thread and implement Runnable at the same time like:

class myThread extends Thread implements Runnable{

}
Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
Yes, it can. BTW, 'extends' must appear before 'implements'.

Take a look at the following code:

Output:
In start
In run
In run
In run

Also, take a look at the following code snippet to understand how start(), run() method calls are interpreted:

Prints : MyThread: start() followed by MyRunnable:run()

Hope this helps

[ July 03, 2005: Message edited by: Kedar Dravid ]

[ July 03, 2005: Message edited by: Kedar Dravid ]
[ July 03, 2005: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Note that Thread does implement interface Runnable. It has a default implementation of the run() method which does nothing. By extending Thread you implement Runnable too (you get the run() implementation by inheritance). By overriding run() you provide your classes' specific implementation of Runnable.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Kedar, why on earth are you providing a start() method?
Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
Well, obviously there's no real need to provide a start() method, just as there'd probably be no need to have a class that extends Thread and mplements Runnable.
I have provided a start() method just so that a person reading this post really understands how start(), run() are interpreted.
Of course, it might have made the example a bit complicated, but I had had a similar doubt while preparing for the SCJP exam, and understanding the output of these two examples made my life easier. In fact, at an interview that I attended some time back, I was asked this exact same question (the one posted by Sangita, that is ).
[ July 03, 2005: Message edited by: Kedar Dravid ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Ah.. the problem is when you override start() in a class extending Thread, then the Thread part of the object will not get started (unless you call super.start()).
reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
Originally posted by Kedar Dravid:

Prints : MyThread: start() followed by MyRunnable:run()

Hope this helps


Could you please tell me why it prints 'MyThread: start()' instead of 'MyThread: run()' for the first class that extends Thread?

Thanks


SCJA, SCJP5.0, SCBCD, SCWCD
Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
In the first class, I have overridden the start() method without a call to super.start(). Because of this, the overridden start() method is called.
reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
I see, so for class that imlements Thead, the overriden start() method does not affect the running of thread is because it has to pass its object to the Thread constructor, the object from there has no overriden start(), therefore it run the run() method.

One more question, under this situation, 'In the first class, I have overridden the start() method without a call to super.start().' -- would this create a new thread or 'MyThread: start()' is printed by main thread?

Thank you.
Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
If you add the statement super.start() in the start() method of the first class, here's what you would get as the o/p:
MyThread: start()
MyThread: run()
MyRunnable: run()
That is, super.start() would actually cause the "thread" part (read: "run()") to execute. Of course, the run() method would execute after the start() method.
reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
Originally posted by reubin nibuer:

One more question, under this situation, 'In the first class, I have overridden the start() method without a call to super.start().' -- would this create a new thread or 'MyThread: start()' is printed by main thread?


Could you also answer this question? Thank you
Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
Here we go again:
I have modified the code in my first post:

class MyThread extends Thread
{
public void run()
{
System.out.println("MyThread: run()");
}
public void start()
{
super.start();
System.out.println("MyThread: start()");
}
}

class MyRunnable implements Runnable
{
public void run()
{
System.out.println("MyRunnable: run()");
}
public void start()
{
System.out.println("MyRunnable: start()");
}

}

public class MyTest
{
public static void main(String args[])
{
MyThread myThread = new MyThread();
MyRunnable myRunnable = new MyRunnable();
Thread thread1 = new Thread(myThread);
Thread thread2 = new Thread(myRunnable);

thread1.start();
thread2.start();}
}

In this case, the o/p is:
MyThread: run()
MyRunnable: run()


In this case, as you might observe, the run() method from both classes is executed. In fact, even if I comment out the statement super.start(), I'd get the same o/p.

Moral of the story:
Thread class implements Runnable interface.

1) If you create a new Thread object and pass an object of a class that extends Thread class as the parameter to this class's constructor, the run() method would be the one to execute, irrespective of whether you provide super.start() or not.

2) If you create a new Thread object from a class which extends Thread class and which has an overridden start() method, the start() method would be the one to execute.
In this case, if you provide a super.start() statement, the run() method too would be executed.

I hope this makes matters clearer.
BTW, the SCJP exam doesn't have questions of this nature, but you never know...

reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
I got it. Thank you for your long typing
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread