• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Invoking run() method of Thread(Runnable r)

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

Can anyone please explain, How to possible the following th.run() method invocation on Runnable implementation?




The output is: running
running
running

Thanks in Advance.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ali,

run() method is simple a method;
it can be called using the reference variable of the class,
(a class that implements Runnable or extends Thread class).

Even if your thread is dead, it can be used to call the instance
method of your class;


You can't call start() on a reference more that once; for that
you have to re reference that variable to new object, like
th= new Thread();
th.start();


Regards,
cmbhatt
[ April 16, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ali,

I would like to mention, what you need to know:

1. What is an Anonymous class.
2. Thread class constructors.
 
m ali
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to Chandra & Srinivasan

My Doubt is "How the Thread object refernce(th) used to call the MyRunnable's method run()?",Please Explain? I agree that its legal to call run() method directly on Thread instance and Runnable Implementation Instance.

Thank you.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ali,

Thread th=new Thread(new MyRunnable());

step 1> new MyRunnable() will create an object of MyRunnable.
Note: Through runnable you can explicitly mention "Thread of excecution".

step 2> new Thread(new MyRunnable());

without Thread Object there is no use of Thread of execution.
Now thread object initialyzed with new "Thread of excution".


step 3> Thread th=new Thread(new MyRunnable());
I got another doubt:
th.run() should call the default run method in Thread(i expected..> but Runnable run() executed? How it blocking default run()?)
th.start() should call the Runnable run()( this is ok, becuase
In start method it can check for Runnable Object and call its run())

Note: Thread object contains default run() method which does nothing. If you don't provide a seperate "thread of execution" through Runnable, then the default one will get called

 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

My guess to my own doubt:

Given >>>
Thread th=new Thread(new MyRunnable());
th.run(); // calls Runnable run() ? How?

Guess: Thread classes default run() method should have included a check for Runnable Object, sothat it can call Runnable run().

What you people think ?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that only calling start will actually create a new thread; calling run (which you should not do) will not - it is executed in the current thread.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

Surpraisingly i found my Guess correct in Thread.java, run()

Here it is default run:

public void run()
671 {
672 if (runnable != null)
673 runnable.run();
674 }

Cheers to me
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ulf,

The Question asked tests both

Thread Object, Thread of Exceution (run()).

Thanks for your Focus.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look at the source code for the Thread class, you will find the following

 
m ali
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sreenivasan
 
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic