• 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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");}

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
Ranch Hand
Posts: 2908
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic