• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Thread query

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
Consider the following classes are in MyThread.java, YourThread.java, and Driver.java files:
public class MyThread implements Runnable{
public void run(){
System.out.println("Running MyThread");
}
}//end of MyThread
public class YourThread extends Thread{
public YourThread(Runnable r){
super(r);
}
public void run(){
System.out.println("Running YourThread");
}
}//end of YourThread
public class Driver{

public static void main(String args []){
MyThread t1= new MyThread();
YourThread t2 = new YourThread(t1);
t2.start();
}
}//end of class
If you try to run Driver class what will be result?

D. It will output "Running MyThread."

A. It will output "Running YourThread."

B. It will output both "Running MyThread," and "Running YourThread."

C. It will not run.
answer is D.IS it correct?If not whats is the answer?
Please explain.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
run() method will be called based on Runnable target
Here target is MyThread class

So..it will execute run() method of MyThread() class
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ankith suresh:
run() method will be called based on Runnable target
Here target is MyThread class

So..it will execute run() method of MyThread() class



Nice bet but wrong.

Look at the documentation of Thread.run(), and you should know why.
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ketki,
MyThread t1= new MyThread();

Look at the below statement

YourThread t2 = new YourThread(t1);

You are passing the reference of MyThread that is t1 so the run() method of MyThread class is executed.Try and execute the code you will understand.

Regards
Nik
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by nik arora:
Try and execute the code you will understand.



Try and execute the code and you will be surprised.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excerpts from Thread.java


Your code:



Don't override the run() method in the YourThread class, you will see the
run() method of the MyThread class to be called.


Thanks,
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandra

I have ran the above code. I am really confused now.Can you explain please?

Thanks
[ June 13, 2007: Message edited by: Bharat Makwana ]
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Bharat

In the Thread class there is a constructor Thread(Runnable r);

ok

And Thread implements Runnable

Means Thread class has run() method which is from Runnable.

Ok

If you can understand upto now the remaining is simple


Replace the Driver class with the below

public class Driver{
public static void main(String args [])
{
MyThread t1= new MyThread();
Thread t=new Thread(t1);// line1
t.start();
YourThread t2 = new YourThread(t1);
t2.start();
}}


ONE THING REMEBER:

There is no start() method in the Runnable.Ok.But we have to start() a new thread ,but how?

That's there is a constuctor called Thread(Runnable r);

If you do like we are saying that i am giving you a class which implements Runnable and it has a method called run() and execute that one,rather the one which has in Thread class.


Thanks

Anil Kumar
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bharat,

have a look at the Thread.run() method:

The method looks whether there is a Runnable object, and if yes, it calls the run method of the Runnable.

This is the normal behaviour of the Thread class.

Our example is a little bit different.

The run method is overridden, and as consequence, the run method of MyThread is never called.

There are two possibilities to ensure that the run method of MyThread is used.

  • Remove the run method from YourThread
  • Call the run method of Thread. Something like this:

  •  
    Chandra Bhatt
    Ranch Hand
    Posts: 1710
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    One more thing I would like to add to Anil's description:

    Thread t1 = new Thread(MyRunnable r){}
    It means t1.start() will cause the MyRunnable's run() to be called.

    If you have a class MyThread that extends from Thread, even if you pass
    MyRunnable to the constructor of the MyThread that does super(myrunnable),
    the run() overridden by the MyThread will be called.

    Your MyRunnable's run method will only be called when you don't override the
    run() method in the MyThread class, and call super(myrunnable) from the
    MyThread class that means passing Runnable to Thread class constructor
    that initializes it "target" (see the above post).


    Ultimately the run() method is called when target!=null.


    Thanks,
     
    Bharat Makwana
    Ranch Hand
    Posts: 107
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thank you guys for your explanation.
     
    Ranch Hand
    Posts: 55
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I was still confused so i thought of recreating the Thread class and Runnable interface and how the run method gets over ride and by doing this i understood the beauty of threading (check it out)


    Check your self

    ----------------------------------------------------------------
    NOTE : Thread -(as) thread1
    Runnable (as) ram
    run() (as) disp()
    start() (as) disp() cannot reproduce that level

    ------------------------------------------------------------------

    interface ram
    {
    public void disp();
    }

    class thread1 implements ram
    {
    ram target;
    thread1()
    {
    target=null;
    }
    thread1(ram r)
    {
    target=r;
    }
    public void disp()
    {
    if(target !=null)
    {
    target.disp();
    }
    }
    }

    class test_interface1 implements ram
    {
    public void disp()
    {
    System.out.println("hai 1");
    }
    }

    public class test_interface2 extends thread1
    {
    test_interface2(ram r)
    {
    super(r);
    }
    public void disp()
    {
    System.out.println("hai 2");
    super.disp();

    }
    public static void main(String args[])
    {
    test_interface1 t1=new test_interface1();
    test_interface2 t2=new test_interface2(t1);
    t2.disp();
    }
    }
    [ June 14, 2007: Message edited by: yogesh srinivasan ]
     
    Manfred Klug
    Ranch Hand
    Posts: 377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Nice work.

    Originally posted by yogesh srinivasan:
    start() (as) disp() cannot reproduce that level



    For the simulation it would be enough if start() calls disp().
     
    Story like this gets better after being told a few times. Or maybe it's just a tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic