• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Threads

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in his book, the answer is given as only (a)!
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic