need to know appropriate ans
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Teena_Pathak
Posted On:   Thursday, September 13, 2007 09:22 AM

The following code defines the ThreadSample class. public class ThreadSample implements Runnable { public static void main(String args[]) { System.out.println("This is the ThreadSample object"); } public void run() { while (true) { try { Thread.sleep(100); System.out.println("Thread ran."); } catch (Exception e) {} } } } Referring to the sample code above, how do you start an instance of ThreadSample in a new thread? Choice 1 new ThreadSample().run(); Choice 2 new ThreadSample().main({""}); Choice 3 Thread.new(ThreadSample); Choice 4 new Thread(new ThreadSample()).start();   More>>

The following code defines the ThreadSample class.

public class ThreadSample implements Runnable {
public static void main(String args[]) {
System.out.println("This is the ThreadSample object");
}

public void run() {
while (true) {
try {
Thread.sleep(100);
System.out.println("Thread ran.");
} catch (Exception e) {}
}
}
}
Referring to the sample code above, how do you start an instance of ThreadSample in a new thread?
Choice 1 new ThreadSample().run();
Choice 2 new ThreadSample().main({""});
Choice 3 Thread.new(ThreadSample);
Choice 4 new Thread(new ThreadSample()).start();
Choice 5 Thread.start(new ThreadSample());

   <<Less

Re: need to know appropriate ans

Posted By:   Robert_Lybarger  
Posted On:   Thursday, September 13, 2007 09:37 AM

Option 1 will cause the 'run()' block to execute in the current thread, not a new thread. Option 2 just calls some static method in the instance, but that method does nothing as far as thread operations are concerned. Option 3 creates a new new thread instance, but the constructor on its own doesn't start the thread up. Option 4 creates a new thread with your class' code, then starts it (the right answer). Option 5 tries to call a method variation in Thread that doesn't exist: the start method takes no arguments (compile error).
About | Sitemap | Contact