• 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

Thread

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can a class extend Thread and implement Runnable at the same time like:

class myThread extends Thread implements Runnable{

}
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()).
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it. Thank you for your long typing
 
reply
    Bookmark Topic Watch Topic
  • New Topic