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 Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads" Watch "Threads" New topic
Author

Threads

Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
Given that a static method doIt() in a class Work represents work to be done, what block of code will succeed in starting a new thread that will do the work?
CODE BLOCK A:
Runnable r = new Runnable() {
public void run() {
Work.doIt();
}
};
Thread t = new Thread(r);
t.start();
CODE BLOCK B:
Thread t = new Thread() {
public void start() {
Work.doIt();
}
};
t.start();
CODE BLOCK C:
Runnable r = new Runnable() {
public void run() {
Work.doIt();
}
};
r.start();
CODE BLOCK D:
Thread t = new Thread(new Work());
t.start();
CODE BLOCK E:
Runnable t = new Runnable() {
public void run() {
Work.doIt();
}
};
t.run();
Answers are not given. This is from Khalid Mock. I say A, B and E. What do u say?
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
in his book, the answer is given as only (a)!


Amir
Alan Chong
Ranch Hand

Joined: Jun 05, 2002
Posts: 106
Hi Deepali,
You seem to be the most hard-working one here
since you have posted a lot.
I agree with you that B and E should be able to
cause DoIt() to be invoked, though they are not the proper ways of using thread.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
A. OK, we create an anonymous inner class that implements the Runnable interface. Then we create a Thread with an instance of the Runnable implementation as target.
B. OK but NEVER DO THAT!! The start() method of the Thread class is natively implemented and should not be overridden. When invoking start() on a thread instance, the scheduler will schedule the target thread for execution. By overriding start(), your thread won't run but just execute the body of the overriding start method.
C. NOT OK because the Runnable interface does not have any start() method.
D. NOT OK because we have no information whether the Work class implements Runnable or not or extends Thread or not.
E. OK but NEVER DO THAT EITHER!!! By invoking run(), you only execute the body of the run method without scheduling your thread.
Bottom lines:
- A thread will be scheduled by invoking start() on it;
- When run, the thread executes the body of the run() method;
- NEVER override the start() method;
- put all the work to be done within the run() method;
- DO NOT invoke run() directly, the scheduler will do that for you.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
...what block of code will succeed in starting a new thread...

Doesn't the question ask which threads are actually started? Neither B & E cause a separate thread to execute, right?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
That's right Paul, I didn't read carefully, d**m it
To summarize:
A. true
B. false (no thread is started)
C. false
D. false (no information on class Work)
E. false (no thread is started)
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
Hehe, nice to know I haven't lost my touch yet, even though I haven't been assigned any Java projects yet. I'm still stuck with good 'ol C for now.
Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
Not really workin hard just doin some last minute preparation for exams.
Hope i am not bothering u guys with silly stuff.
Thnx
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads