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 Thread Question 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 "Thread Question" Watch "Thread Question" New topic
Author

Thread Question

Matt Ghiold
Ranch Hand

Joined: Feb 24, 2002
Posts: 213
Guys,
This question is from JQ+ trial edition:
Question ID :958150528594
[TRIAL]
Consider the following class...
public class Worker implements Runnable
{
Thread w;
public void start()
{
w = new Thread(this);
}
public void run()
{
System.out.println(" Working...");
}
/* lot of code */
}
Assume that the class compiles fine.
What will happen if a Worker object is created and it's start method is called?
Answers:
1. It will keep on printing "Working..."
2. It will not print anything
3. It will print "Working..." just once.
4. It will print "Working..." til stop() is called on the Worker object
5. None of the above
The answer is 2, but I want to know why the
answer is not 3.
If you create a Worker object like the questions indicates to then, then you call it's start method
you should get answer 3
For example:
Thread y = new Thread(new Worker);
y.start();
Or is the question saying to call start one the Worker object itself, which won't do anything, because it doesnt implement thread?


-Matt<br /> SCJP2<br /> SCJD
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
If you take another look at the Worker class again, you'll see it has defined it's own start() method which does nothing but creates a Thread object!


<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
Also one other thing about the code, if a worker object is created, then it must be assigned to a thread object in order to be able to run on a separate thread.
Note that the Worker class implements the Runnable interface and does not extend the Thread class.
[ March 01, 2002: Message edited by: Rajinder Yadav ]
Matt Ghiold
Ranch Hand

Joined: Feb 24, 2002
Posts: 213
My apologies, I should be in bed, to much Java / not enough sleep is making me ask very stupid questions
Thank's a lot for humoring me and helping out!!
Macon Pegram
Greenhorn

Joined: Mar 01, 2002
Posts: 12
The code in it's current state is fine. It's perfectly acceptable to assume that this code will run as a thread.
The only problem with the code (and the reason it does nothing) is that you must do more than simply create a thread.. you must start it. In order for the code to actually output "Working..." your start() method would have to look like this:
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
Macon, you're absolutely correct! I missed that detail
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Question
 
Similar Threads
java callback
Thread Ques
From Velmurugan's Notes
Multi-threading: doesn't the explanation contradict the answer?
Java Thread's reference