public class Q126 implements Runnable { private int x; private int y; public static void main(String [] args) { Q126 that = new Q126(); (new Thread(that)).start( ); /* Line 8 */ (new Thread(that)).start( ); /* Line 9 */ } public synchronized void run( ) /* Line 11 */ { for (;;) /* Line 13 */ { x++; y++; System.out.println("x = " + x + "y = " + y); } } }
Also note that because of the infinite loop at line 13, only one thread ever gets to execute.
public class WaitTest { public static void main(String [] args) { System.out.print("1 "); synchronized(args) { System.out.print("2 "); try { args.wait(); /* Line 11 */ } catch(InterruptedException e){ } } System.out.print("3 "); } }
A is incorrect; IllegalMonitorStateException is an unchecked exception so it doesn't have to be dealt with explicitly.
B and C are incorrect; 3 will never be printed, since this program will never terminate because it will wait forever.
public class SyncTest { public static void main (String [] args) { Thread t = new Thread() { Foo f = new Foo(); public void run() { f.increase(20); } }; t.start(); } } class Foo { private int data = 23; public void increase(int amt) { int x = data; data = x + amt; } }and assuming that data must be protected from corruption, what?if anything?can you add to the preceding code to ensure the integrity of data?
Option A is incorrect because synchronizing the run() method would stop other threads from running the run() method (a bad idea) but still would not prevent other threads with other runnables from accessing the increase() method.
Option B is incorrect for virtually the same reason as A?synchronizing the code that calls the increase() method does not prevent other code from calling the increase() method.
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(); } }
The updating and output takes place inside synchronized code.
One thread will run to completion printing the numbers 1-10.
The second thread will then run to completion printing the numbers 11-20.
public class Test { public static void main (String [] args) { final Foo f = new Foo(); Thread t = new Thread(new Runnable() { public void run() { f.doStuff(); } }); Thread g = new Thread() { public void run() { f.doStuff(); } }; t.start(); g.start(); } } class Foo { int x = 5; public void doStuff() { if (x < 10) { // nothing to do try { wait(); } catch(InterruptedException ex) { } } else { System.out.println("x is " + x++); if (x >= 10) { notify(); } } } }
A, B are incorrect because the code compiles without errors.
D is incorrect because the exception is thrown before there is any output.
class Test116 { static final StringBuffer sb1 = new StringBuffer(); static final StringBuffer sb2 = new StringBuffer(); public static void main(String args[]) { new Thread() { public void run() { synchronized(sb1) { sb1.append("A"); sb2.append("B"); } } }.start(); new Thread() { public void run() { synchronized(sb1) { sb1.append("C"); sb2.append("D"); } } }.start(); /* Line 28 */ System.out.println (sb1 + " " + sb2); } }
add this code after line 28:
try { Thread.sleep(5000); } catch(InterruptedException e) { }
and you have some chance of predicting the outcome.
class MyThread extends Thread { public static void main(String [] args) { MyThread t = new MyThread(); t.start(); System.out.print("one. "); t.start(); System.out.print("two. "); } public void run() { System.out.print("Thread "); } }
class s1 implements Runnable { int x = 0, y = 0; int addX() {x++; return x;} int addY() {y++; return y;} public void run() { for(int i = 0; i < 10; i++) System.out.println(addX() + " " + addY()); } public static void main(String args[]) { s1 run1 = new s1(); s1 run2 = new s1(); Thread t1 = new Thread(run1); Thread t2 = new Thread(run2); t1.start(); t2.start(); } }
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(); } }
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.
Copyright ©CuriousTab. All rights reserved.