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
Joined: Feb 28, 2007
Posts: 1707
posted
0
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();
1. What is an Anonymous class. 2. Thread class constructors.
Thanks & Regards,<br />T.Srinivasan,<br />SCWCD 1.4(89%),SCJP 5.0(75%)<br />"That service is the noblest which is rendered for its own sake." - Mahatma Gandhi
m ali
Ranch Hand
Joined: Apr 12, 2007
Posts: 49
posted
0
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
Joined: Feb 15, 2007
Posts: 557
posted
0
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