Tuesday, August 17, 2010

Multithreading: NoGo.java

5. class NoGo implements Runnable {
6.   private int i;
7.   public synchronized void run() {
8.     if (i%10 != 0) { i++; }
9.     for(int x=0; x<10; x++, i++)
10.       { if (x == 4) Thread.yield(); }
11.     System.out.print(i + " ");
12.   }
13.   public static void main(String [] args) {
14.     NoGo n = new NoGo();
15.     for(int x=0; x<101; x++) { new Thread(n).start(); }
16.   }
17. }

Which is true?


A     The output can never contain the value 10.
B     The output can never contain the value 30.
C     The output can never contain the value 297.
D     The output can never contain the value 820.
E     The output can never contain the value 1010.


C is correct. Because run is synchronized, only values that are divisible by 10, up to 1010, are possible.

No comments:

Post a Comment