• 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

Threads Doubt

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic