File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads Doubt

 
gobburi saikrishna
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");}
5)}

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

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

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
Posts: 2905
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
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.

regardz;
Sagar
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic