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

Khalid Mock Exam Garbage collection

 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi All
there are few questions from Khaalid Mock Exam
Since answers are not provided I would be thnakful if you give your views
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();
a.Code block A
b.Code block B
c.Code block C
d.Code block D
e.Code block E

Only b is correct according to me as we cannot instaniate an Interface
what fo you all say ?
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Gaurav,
Here is my two cents worth...


Yes, this will work.
Here, we are creating an anonymous class implementing the Runnable
interface. Note the run method that has been implemented which calls
the Work.doIt(). Hence the thread will execute the doIt method.

Nope. This will not work. When you implement a thread by extending the Thread
class, you are required to override the run() method and not the start method.
I know one might argue because we have an overridden version of start() method
here, t.start will execute Work.doIt(). But the catch is, it will be just a
normal synchronous method invocation, and it will not start a new thread of
execution
.


Nope.
This answer will trick you into thinking it might be the right one. Look
carefully - though we are implementing a Runnable interface, you are not
creating a new Thread object. Runnable only defines the run() method
and hence this code will not even compile!

Nope.
Wrong syntax. We don't know if Work extends thread, and even if it does,
we only know it contains a static method doIt(). We are not sure if it
implements the run() method. Too many things to assume.....

Nope.
This is not the correct way of starting a new thread. Though the first
part of setting up the run method is done correctly here, note that no
thread objects are created!
Hope this helps. If not, feel free to ask for more

Ajith
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
The same question is being discussed here http://www.javaranch.com/ubb/Forum24/HTML/002667.html under the right title.
Just to avoid confusion, I am closing this thread.
Ajith
 
Good night. Drive safely. Here's a tiny ad for the road:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    Bookmark Topic Watch Topic
  • New Topic