wood burning stoves
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Doubt 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 "Threads Doubt" Watch "Threads Doubt" New topic

Threads Doubt

gobburi saikrishna
Ranch Hand

Joined: Jan 21, 2008
Posts: 52
Hello Ranchers
please help me to slove the program below

1)class MyThread extends Thread {
2) MyThread(){}
3)MyThread(Runnable r) {super(r);}
4)public void run() {System.out.print("Inside thead");}

6)class MyRunnable implements Runnable {
7) public void run() {System.out.print("Inside runnable");}

9)public class Test {
10) public static void main(String[] args){
11) new MyThread().start()
12) new MyThread(new MyRunnable()).start();

when i compile and Run iam getting output as
Inside thead Inside thead
my doubt is at line 12 Runnabler version of start() is called, and output
should be Inside thead Inside Runnable
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2904

If you know how constructor calls happened , when an object is created , then its has simple solution .

at line 12

1) MyThread`s parameterized constructor gets called .
which call super() i.e Thread class`s parameterized constructor gets called

2) secondly and most importantly it call run method of MyThread`s object , whic output second "Inside thead".

Hope you ll get this.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
I agree. Here's the link: http://aspose.com/file-tools
subject: Threads Doubt
It's not a secret anymore!