Hi,
I don't quite understand the following
Java Thread question posted at:
http://www.certgear.com/products/preview/SCJP/index.html?java http://www.certgear.com/products/info/scjp.htm?java My understanding is that the this.notify () method will un-block a thread that is waiting due to a this.wait (). However, this does NOT appear to be the case in the following thread code. Can someone explain?
---------------------------------------------------------------
Consider the following multi-threaded code that users inner-class declarations to create and execute multiple threads. What will be the output of the code?
1: public class
test 2: {
3: public static void main (
String args[])
4: {
5: final String s="s";
6: final Thread t= new Thread (new Runnable ()
7: {
8: public void run () {
9: try
10: {
11: for (int i=1; i < 5; i++)
12: {
13: synchronized (this)
14: {
15: System.out.print (" " + i);
16: this.wait ();
17: }
18: }
19: } catch (Exception ex) { ex.printStackTrace (); }
20: }
21: });
22:
23: Thread t2 = new Thread (new Runnable ()
24: {
25: public void run () {
26: while (true)
27: {
28: synchronized (this) { this.notify (); }
29: }
30: }
31: });
32: t.start ();
33: t2.start ();
34: }
35:}
A) A compilation error will occur
B) 1 2 3 4 will be printed
C) A run-time error will be thrown
D) Nothing will be printed to the output
E) 1 will be printed
[Ans: E]
[ February 24, 2005: Message edited by: Sunil Krishnamuthy ]