Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Threads" Watch "Doubt in Threads" New topic
Author

Doubt in Threads

siva prasaad
Greenhorn

Joined: Jan 10, 2007
Posts: 23
Hi

In java , we can implement the concept of thread , through the Thread object
and the Thread object can be created by either extending Thread Class or
implementing the Runnable Interface
In case of Runnable Interface, it has run() method , where actuall execution is done
And for this , as per the concept we should create a Runnable Object for this and should be sent as a parameter to the Thread Class Construcor


My doubt is
why should we send the Runnable Object as parameter

In general , Whenever a class imlements the Interface , it can directly use the method without the interface object...

Thanks
Siva
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by siva prasaad:
My doubt is why should we send the Runnable Object as parameter

In general , Whenever a class imlements the Interface , it can directly use the method without the interface object...

If you call the run method directly, you don't have a second thread of execution. Have a look at the following example:The output is:
Running on thread: main
Running on thread: main
Running on thread: Thread-0
siva prasaad
Greenhorn

Joined: Jan 10, 2007
Posts: 23
Hi Manfred
In the program,you are not calling the run() method after the following code.
new Thread(r).start();

So obviously , it would not execute for second thread.

Also , How Runnable object is going to overcome the problem you mentioned.

Thanks
Siva
Jeremy Botha
Ranch Hand

Joined: Feb 16, 2005
Posts: 125
If you took the time to examine the API for the Thread class, you'd realise that Thread will execute the run() method of any Runnable object passed to it.


Hi Manfred
In the program,you are not calling the run() method after the following code.
new Thread(r).start();

So obviously , it would not execute for second thread.


As stated above, your assumption is incorrect. The run() method is called, just not inside any code reachable by your application.

J


McFinnigan? Never heard of him. Nobody here but us chickens...<br /> <br />SCJP for Java 1.4<br />SCJD for Java 5.0
siva prasaad
Greenhorn

Joined: Jan 10, 2007
Posts: 23
Thansx Guys for your inputs...

Iam putting in brief , what i understood.
Please correct me if iam missing anything.

So ,
Thread is an independent path of execution.
For this Thread object should be created.

Now the actual work is done in run() method.

If a class implements an interface, the object of this class is called Runnable Object.

Now it is something like whichever Thread wants to perform the work, it just uses the Runnable Object to perform its job.

Is this fine.......

Thanks
Siva
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Threads
 
Similar Threads
thread with no parameters doubt
Doubt
doubt in serializatiion
doubt in threads
Interface doubt