• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Invoking run() method of Thread(Runnable r)

 
m ali
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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: 557
  • 0
  • Mark post as helpful
  • send pies
  • 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: 557
  • 0
  • Mark post as helpful
  • send pies
  • 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 ?
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • 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: 557
  • 0
  • Mark post as helpful
  • send pies
  • 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: 557
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf,

The Question asked tests both

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

Thanks for your Focus.
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sreenivasan
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic