File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads

 
Deepali Pate
Ranch Hand
Posts: 114
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in his book, the answer is given as only (a)!
 
Alan Chong
Ranch Hand
Posts: 106
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Villangca
Ranch Hand
Posts: 133
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...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
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really workin hard just doin some last minute preparation for exams.
Hope i am not bothering u guys with silly stuff.
Thnx
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic