File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Invoking run() method of Thread(Runnable r) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Invoking run() method of Thread(Runnable r) " Watch "Invoking run() method of Thread(Runnable r) " New topic
Author

Invoking run() method of Thread(Runnable r)

m ali
Ranch Hand

Joined: Apr 12, 2007
Posts: 49
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

Joined: Feb 28, 2007
Posts: 1707
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 ]

cmbhatt
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Ali,

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

1. What is an Anonymous class.
2. Thread class constructors.


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
m ali
Ranch Hand

Joined: Apr 12, 2007
Posts: 49
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
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

Joined: Feb 15, 2007
Posts: 557
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
Marshal

Joined: Mar 22, 2005
Posts: 42946
    
  68
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

Joined: Feb 15, 2007
Posts: 557
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

Joined: Feb 15, 2007
Posts: 557
Hi Ulf,

The Question asked tests both

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

Thanks for your Focus.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
If you look at the source code for the Thread class, you will find the following

m ali
Ranch Hand

Joined: Apr 12, 2007
Posts: 49
Thanks Sreenivasan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Invoking run() method of Thread(Runnable r)