Option B is wrong. The start() method causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
Option C is wrong. The stop() method is deprecated. It forces the thread to stop executing.
Option D is wrong. Is the main entry point for an application.
wait(2000);
After calling this method, when will the thread A become a candidate to get another turn at the CPU?
Option B is incorrect because a waiting thread will not return to runnable when the lock is released, unless a notification occurs.
Option C is incorrect because the thread will become a candidate immediately after notification, not two seconds afterwards.
Option D is also incorrect because a thread will not come out of a waiting pool just because a lock has been released.
Option A is wrong. There is no init() method in the Thread class.
Option C is wrong. The run() method of a thread is like the main() method to an application. Starting the thread causes the object's run method to be called in that separately executing thread.
Option D is wrong. The resume() method is deprecated. It resumes a suspended thread.
Option B is wrong. notify() - wakes up a single thread that is waiting on this object's monitor.
Option C is wrong. notifyAll() - wakes up all threads that are waiting on this object's monitor.
Option D is wrong. Typically, releasing a lock means the thread holding the lock (in other words, the thread currently in the synchronized method) exits the synchronized method. At that point, the lock is free until some other thread enters a synchronized method on that object. Does entering/exiting synchronized code mean that the thread execution stops? Not necessarily because the thread can still run code that is not synchronized. I think the word directly in the question gives us a clue. Exiting synchronized code does not directly stop the execution of a thread.
public class MyRunnable implements Runnable { public void run() { // some code here } }
A is incorrect. There is no constructor like this for Runnable because Runnable is an interface, and it is illegal to pass a class or interface name to any constructor.
B is incorrect for the same reason; you can't pass a class or interface name to any constructor.
D is incorrect because MyRunnable doesn't have a start() method, and the only start() method that can start a thread of execution is the start() in the Thread class.
class Happy extends Thread { final StringBuffer sb1 = new StringBuffer(); final StringBuffer sb2 = new StringBuffer(); public static void main(String args[]) { final Happy h = new Happy(); new Thread() { public void run() { synchronized(this) { h.sb1.append("A"); h.sb2.append("B"); System.out.println(h.sb1); System.out.println(h.sb2); } } }.start(); new Thread() { public void run() { synchronized(this) { h.sb1.append("D"); h.sb2.append("C"); System.out.println(h.sb2); System.out.println(h.sb1); } } }.start(); } }
class MyThread extends Thread { public static void main(String [] args) { MyThread t = new MyThread(); /* Line 5 */ t.run(); /* Line 6 */ } public void run() { for(int i=1; i < 3; ++i) { System.out.print(i + ".."); } } }
A is incorrect because line 5 is the proper way to create an object.
B is incorrect because it is legal to call the run() method, even though this will not start a true thread of execution. The code after line 6 will not execute until the run() method is complete.
D is incorrect because the for loop only does two iterations.
public class Test107 implements Runnable { private int x; private int y; public static void main(String args[]) { Test107 that = new Test107(); (new Thread(that)).start(); (new Thread(that)).start(); } public synchronized void run() { for(int i = 0; i < 10; i++) { x++; y++; System.out.println("x = " + x + ", y = " + y); /* Line 17 */ } } }
System.out.println(Thread.currentThread().getName() + " x = " + x + ", y = " + y);
class MyThread extends Thread { MyThread() {} MyThread(Runnable r) {super(r); } public void run() { System.out.print("Inside Thread "); } } class MyRunnable implements Runnable { public void run() { System.out.print(" Inside Runnable"); } } class Test { public static void main(String[] args) { new MyThread().start(); new MyThread(new MyRunnable()).start(); } }
In this case, however, the run method in the Thread class is overridden by the run method in MyThread class. Therefore the run() method in MyRunnable is never invoked.
Both times, the run() method in MyThread is invoked instead.
class MyThread extends Thread { public static void main(String [] args) { MyThread t = new MyThread(); Thread x = new Thread(t); x.start(); /* Line 7 */ } public void run() { for(int i = 0; i < 3; ++i) { System.out.print(i + ".."); } } }
Option A is incorrect because the Thread class implements the Runnable interface; therefore, in line 7, Thread can take an object of type Thread as an argument in the constructor.
Option B and C are incorrect because the variable i in the for loop starts with a value of 0 and ends with a value of 2.
Copyright ©CuriousTab. All rights reserved.