Given: public class SyncTest { private int x; private int y; private synchronized void setX(int i) {x = 1;} private synchronized void setY(int i) {y = 1;} public void setXY(int i){setX(i); setY(i);} public synchronized Boolean check() {return x != y;} } Under which conditions will check() return true when called from a different class?
Exhibit: class Q implements Runnable { int i; public void run () { try { Thread.sleep(5000); i = 10; } catch(InterruptedException e) {} } } 10 class Test { public static void main(String args[]) { try { Q a = new Q(); Thread t = new Thread(a); t.start(); int j = a.i; System.out.println("The value of j is: " + j); } catch (Exception e) {} } } Which statement after ‘t.start(); ’ will ensure that j = 10 ?
Given: public class Q implements Runnable { private int x; private int y; 04 public static void main(String [] args) { Q that = new Q(); (new Thread(that)).start( ); (new Thread(that)).start( ); } 10 public synchronized void run( ) { for (;;) { x++; y++; System.out.println("x = " + x + "y = " + y); } } } What is the result?
Given: public class Q126a implements Runnable{ private int x; private int y; 04 public static void main(String[] arg){ Q126a that = new Q126a(); (new Thread(that)).start(); (new Thread(that)).start(); } 10 public void run(){ for(;;){ x++; y++; System.out.println("x = " + x + " y = " + y); } } } What is the result?
Which two CANNOT directly cause a thread to stop executing? (Choose Two)
Given: public class Q128{ public static void main(String[] args) { final StringBuffer s1= new StringBuffer(); final StringBuffer s2= new StringBuffer(); 05 new Thread () { public void run() { synchronized(s1) { s1.append("A"); synchronized(s2) { s2.append("B"); System.out.print(s1); System.out.print(s2); } } } }.start(); 18 new Thread() { public void run() { synchronized(s2) { s2.append("C"); synchronized(s1) { s1.append("D"); System.out.print(s2); System.
Which method in the Thread class is used to create and launch a new thread of execution?
What will happen when you attempt to compile and run the following code? public class ThreadDemo { private int count = 1; public synchronized void doSomething() { for (int i = 0; i < 10; i++) System.out.println(count++); } public static void main(String[] args) { ThreadDemo demo = new ThreadDemo(); Thread a1 = new A(demo); Thread a2 = new A(demo); a1.start(); a2.start(); } } class A extends Thread { ThreadDemo demo; public A(ThreadDemo td) { demo = td; } public void run() { demo.doSomething()
What is the result of compiling and executing the following code? 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(); } }
What will be the result of trying to compile and run the following code? 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(); } }