Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread

 
Sangita Mishra
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a class extend Thread and implement Runnable at the same time like:

class myThread extends Thread implements Runnable{

}
 
Kedar Dravid
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kedar, why on earth are you providing a start() method?
 
Kedar Dravid
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kedar Dravid
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it. Thank you for your long typing
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic