Java Programming :: Threads
-
What will be the output of the program?
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 Test
{
public static void main(String [] args)
{
printAll(args);}
public static void printAll(String[] lines)
{
for(int i = 0; i{
System.out.println(lines[i]);
Thread.currentThread().sleep(1000);
}
}
}the static method Thread.currentThread() returns a reference to the currently executing Thread object. What is the result of this code?
-
What will be the output of the program?
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 + ".."); } } }
-
What will be the output of the program?
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); } }
-
What will be the output of the program?
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 output of the program?
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 */ } } }
-
What will be the output of the program?
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(InterruptedExcepx) { } } else { System.out.println("x is " + x++); if (x >= 10) { notify(); } } } }
-
What will be the output of the program?
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 + ".."); } } }
A.
Each String in the array lines will output, with a 1-second pause. |
B.
Each String in the array lines will output, with no pause in between because this method is not executed in a Thread. |
C.
Each String in the array lines will output, and there is no guarantee there will be a pause because currentThread() may not retrieve this thread. |
D.
This code will not compile. |
A.
Compilation error. |
B.
Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by both threads running simultaneously. |
C.
Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by first one thread then the other. This is guaranteed by the synchronised code. |
D.
Will print in this order x = 1 y = 2 x = 3 y = 4 x = 5 y = 6 x = 7 y = 8... |