(6) is correct because wait()/notify()/notifyAll() must all be called from within a synchronized, context. A thread must own the lock on the object its invoking wait()/notify()/notifyAll() on.
(1) is incorrect because wait()/notify() will not prevent deadlock.
(2) is incorrect because a sleeping thread will return to runnable when it wakes up, but it might not necessarily resume execution right away. To resume executing, the newly awakened thread must still be moved from runnable to running by the scheduler.
(3) is incorrect because synchronization prevents two or more threads from accessing the same object.
(5) is incorrect because notify() is not overloaded to accept a duration.
class MyThread extends Thread { MyThread() { System.out.print(" MyThread"); } public void run() { System.out.print(" bar"); } public void run(String s) { System.out.println(" baz"); } } public class TestThreads { public static void main (String [] args) { Thread t = new MyThread() { public void run() { System.out.println(" foo"); } }; t.start(); } }
class s1 extends Thread { public void run() { for(int i = 0; i < 3; i++) { System.out.println("A"); System.out.println("B"); } } } class Test120 extends Thread { public void run() { for(int i = 0; i < 3; i++) { System.out.println("C"); System.out.println("D"); } } public static void main(String args[]) { s1 t1 = new s1(); Test120 t2 = new Test120(); t1.start(); t2.start(); } }
public class ThreadTest extends Thread { public void run() { System.out.println("In run"); yield(); System.out.println("Leaving run"); } public static void main(String []argv) { (new ThreadTest()).start(); } }
class Test { public static void main(String [] args) { printAll(args); } public static void printAll(String[] lines) { for(int i = 0; i < lines.length; i++) { System.out.println(lines[i]); Thread.currentThread().sleep(1000); } } }
A is incorrect, but it would be correct if the InterruptedException was dealt with.
B is incorrect, but it would still be incorrect if the InterruptedException was dealt with because all Java code, including the main() method, runs in threads.
C is incorrect. The sleep() method is static, so even if it is called on an instance, it still always affects the currently executing thread.
class s implements Runnable { int x, y; public void run() { for(int i = 0; i < 1000; i++) synchronized(this) { x = 12; y = 12; } System.out.print(x + " " + y + " "); } public static void main(String args[]) { s run = new s(); Thread t1 = new Thread(run); Thread t2 = new Thread(run); t1.start(); t2.start(); } }
Runnable target = new MyRunnable(); Thread myThread = new Thread(target);Which of the following classes can be used to create the target, so that the preceding code compiles correctly?
Option A is incorrect because interfaces are not extended; they are implemented.
Option B is incorrect because even though the class would compile and it has a valid public void run() method, it does not implement the Runnable interface, so the compiler would complain when creating a Thread with an instance of it.
Option D is incorrect because the run() method must be public.
Option A is incorrect - just because another thread activates the modify method in A this does not mean that the thread will automatically resume execution
Option C is incorrect - This is incorrect because as said in Answer B notify only wakes the thread but further to this once it is awake it goes back into the stack and awaits execution therefore it is not a "direct and sole consequence of the notify call"
Option D is incorrect - The notify method wakes one waiting thread up. If there are more than one sleeping threads then the choice as to which thread to wake is made by the machine rather than you therefore you cannot guarantee that the notify'ed thread will be the first waiting thread.
Option B is incorrect because to call wait(), the thread must own the lock on the object that wait() is being invoked on, not the other way around.
Option C is wrong because notify() is defined in java.lang.Object.
Option D is wrong because notify() will not cause a thread to release its locks. The thread can only release its locks by exiting the synchronized code.
A is incorrect because static methods can be synchronized; they synchronize on the lock on the instance of class java.lang.Class that represents the class type.
C is incorrect because only methods?not variables?can be marked synchronized.
D is incorrect because a sleeping thread still maintains its locks.
(1) is correct - Extending the Thread class and overriding its run method is a valid procedure.
(4) is correct - You must implement interfaces, and runnable is an interface and you must also include the run method.
(2) is wrong - Runnable is an interface which implements not Extends. Gives the error: (No interface expected here)
(3) is wrong - You cannot implement java.lang.Thread (This is a Class). (Implements Thread, gives the error: Interface expected). Implements expects an interface.
(5) is wrong - You cannot implement java.lang.Thread (This is a class). You Extend classes, and Implement interfaces. (Implements Thread, gives the error: Interface expected)
Comments
There are no comments.Copyright ©CuriousTab. All rights reserved.