aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes threads 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 "threads" Watch "threads" New topic
Author

threads

Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Hi all.
I'm a little confused by The Complete Java 2 Certification Study Guide (RHE)'s chapter 7 on threads. It says on page 202 that
"If you want your thread to execute the run() method of some object other than itself, you still need to construct an instance of the Thread class. The only difference is that when you call the Thread constructor, you have to specify which object owns the run() method that you want. To do this, you invoke an alternate form of the Thread constructor."
On page 203 they give the following example:

public class DownCounter implements Runnable
{
public void run()
{
for(int i = 10; i >= 1; i--)
System.out.println("Counting Down: " + i);
}
}
DownCounter dc = new DownCounter();
Thread t = new Thread(dc);
t.start();
I don't understand the point of doing this. Won't the same run() method be called as if you had just written
DownCounter dc = new DownCounter();
dc.start();
Why bother with creating the Thread object t?
Please let me know what I am missing.
Thanks in advance.
Stephanie
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
First off, there's no start method in your class. Second, the Thread class version of start contains all of the crazy code to coordinate with the underlying operating system to generate the actual thread. The Thread start method also calls the run method of the Runnable object you passed to its constructor (unless did the extend Thread thing...in which case it calls your overridden run method).
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
This is the concept of thread 'targets'.
Since a thread represents a simultaneous ( and independent ) path of execution, the Thread class provides an overloaded constructor which accepts a target parameter. This will make it possible for virtually any code to start a thread if it knows about the target.
Though it is a normal practice to start a thread from the thread-implementor, I am sure you can think of situations where you might want to do it from a normal class. One such example would be your own worker thread pools that you want to manage from a work-engine which by itself, is not the thread implementor.
Start thinking about it...
Ajith
(PS - "implementor" is a lexically wrong word, but I couldn't find anything close to it!! )


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1859
Hi Stephanie Grasson,
"If you want your thread to execute the run() method of some object other than itself, you still need to construct an instance of the Thread class.
this is done by
Thread t=new Thread(Runnable r)

or
Thread t=new Thread(Thread r);
Both the above work
"The only difference is that when you call the Thread constructor, you have to specify which object owns the run() method that you want. To do this, you invoke an alternate form of the Thread constructor."
This is done by passing the class that implements the Runnable interface or extends a Thread to the Thread creation statement as parameter .
Your specific question is
How is this
a)DownCounter dc = new DownCounter();
Thread t = new Thread(dc);
t.start();
different from
b)DownCounter dc = new DownCounter();
dc.start();
thus Why bother with creating the Thread object t?
The question one has to answer is why create threads.
One creates threads so that a different execution path is created and the current execution is free to continue doing other work
When one says b) what happens(see code below). The start method of the DownCounter class is invoked. As such there is no invokation of the run() method.
When one says a), the start() method of thread is called. This by default calls the run() method of the Runnable Object parameter passed to it while this Thread class is being created.
This starts a different execution path than the one above.

public class DownCounter implements Runnable
{
public void run()
{
for(int i = 10; i >= 1; i--)
System.out.println("Counting Down: " + i);
}
public void start(){
System.out.println("start method");
}
public static void main(String args[]){
DownCounter dc = new DownCounter();
System.out.println("before class start");
dc.start();// does not call run
System.out.println("after class start");
Thread t = new Thread(dc);
t.start();// calls run
}
}
Regds.
Rahul

[This message has been edited by Rahul Mahindrakar (edited August 01, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: threads