• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Threads-mock exam question

 
Greenhorn
Posts: 17
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ranchers...hope all are doing good.......

Which two code fragments will execute the method doStuff() in a separate thread?
(Choose two.)
A. new Thread() {
public void run() { doStuff() ;}
};
B. new Thread() {
public void start() { doStuff(); }
};
C. new Thread() {
public void start() { doStuff(); }
}.run();
D. new Thread() {
public void run() { doStuff(); }
}.start();
E. new Thread(new Runnable() {

public void run() { doStuff(); }
}).run();
F. new Thread(new Runnable() {
public void run() { doStuff(); }
}).start();

Answer: D, F

Could you please explain this question ..................please explain what they are asking, the correct answer and why....i got this from,
http://www.certpractice.com/Free-latest-Certification-Questions-Answers.php?limit=2&exm_name=Sun-Certified-Programmer-for-the-Java-2-Platform,-Standard-Edition-5.0&qid=99&exam_code=
 
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is quite easy .. you have to read more about threads.

A is incorrect because although it syntactically perfect but in order to executed in separate thread, thread must be started. Here it is not started.

B is incorrect. same reason as above but here run method is not overridden so the default implementation contained in thread class fulfils the requirement.

C. incorrect Thread is allocated separate stack only when it's start method is called. here run method is called like any ordinary method.

E same reason as C. here an instance of Runnable interface is given into the costructor of thread which is perfectly legal but start method needs to be called in order to be executed in separate thread.

Now i guess i dont have to tell why D and F are correct.
 
Asher Basheer
Greenhorn
Posts: 17
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes .....you don't have to......thank you
 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic