java - Two threads, same static variable, same value, concurrent access -



i've been trying ready scjp exam have take next week, , i've encountered question java threads.

1-public class stone implements runnable { 2-  static int id = 1; 3- 4-  public void run() { 5-      try { 6-          id = 1 - id; 7-          if (id == 0) { 8-                      pick(); 9-          } else { 10-             release(); 11-         } 12- 13-     } catch (exception e) { 14-     } 15- } 16- 17- private static synchronized void pick() throws exception { 18-     system.out.print("p "); 19-     system.out.print("q "); 20- } 21- 22- private synchronized void release() throws exception { 23-     system.out.print("r "); 24-     system.out.print("s "); 25- } 26- 27- public static void main(string[] args) { 28-     stone st = new stone(); 29-     new thread(st).start(); 30-     new thread(st).start(); 31- } 32-} 
  • which true? (choose apply.)
  • the output p q r s
  • the output p r s q
  • the output p r q s
  • the output p q p q
  • the program cause deadlock.
  • compilation fails.

the answer key says:
a, b, , c correct. since pick() static , release() non-static, there 2 locks. if pick() non-static, correct.

says output p q p q not option , it's not possible such results.

@ beginning, didn't believe answer key, saw it's not possible see output result of application. (after running class.)

now, that's part confuses me little bit, , here why

i thought p q p q or r s r s result must have been possible. because there chance situation makes variable id same both threads. in other words, example, when first thread finished executing line 6 give turn other one, , after that, other 1 change value of variable id , voila! go in same if block happily.

i tried see situation on , on again (with eclipse juno , java 7). doesn't happen. i'm sure there wrong way of thinking, , wonder know is. i need know rule hinders these 2 threads access variable id @ same state.

actually, there quite few possibilities, extremely unlikely, still possible nonetheless , after 1 million executions found.

code:

public class stone implements runnable {     static int id = 1;     static stringbuffer buffer = new stringbuffer();      public void run() {         try {             id = 1 - id;             if (id == 0) {                 pick();             } else {                 release();             }          } catch (exception e) {         }     }      private static synchronized void pick() throws exception {         buffer.append("p ");         buffer.append("q ");     }      private synchronized void release() throws exception {         buffer.append("r ");         buffer.append("s ");     }      public static void main(string[] args) {         int count = 1000000;         map<string, integer> results = new hashmap<string, integer>();         system.out.println("running " + count + " times...");         (int = 0; i< count; i++) {             buffer = new stringbuffer();             stone stone = new stone();             thread t1 = new thread(stone);             thread t2 = new thread(stone);             t1.start();             t2.start();             while (t1.isalive() || t2.isalive()) {                 // wait             }             string result = buffer.tostring();             integer x = results.get(result);             if (x == null) x = 0;             results.put(result, x + 1);             if (i > 0 && % 50000 == 0) system.out.println(i + "... " + results.keyset());         }         system.out.println("done, results were:");         (string key : results.keyset()) {             system.out.println(" " + key + ": " + results.get(key));         }     } } 

results:

running 1000000 times... 50000... [r s p q , p q r s , p r s q , r p q s ] 100000... [r s p q , p q r s , p r s q , r p q s ] 150000... [r s p q , p q r s , p r s q , r p q s ] 200000... [r s p q , p q r s , p r s q , r p q s ] 250000... [r s p q , p q r s , p r s q , r p q s ] 300000... [r s p q , p q r s , p r s q , r p q s ] 350000... [r s p q , p q r s , p r s q , p r q s , r p q s ] 400000... [r s p q , p q r s , p r s q , p r q s , r p q s ] 450000... [r s p q , p q r s , p r s q , p r q s , r p q s ] 500000... [r s p q , p q r s , p r s q , p r q s , r p q s ] 550000... [r s p q , p q r s , p r s q , p r q s , r p q s ] 600000... [r s p q , p q r s , p r s q , p r q s , r p q s ] 650000... [r s p q , p q r s , p r s q , p r q s , r p q s ] 700000... [r s p q , p q r s , p r s q , p r q s , r p q s ] 750000... [r s p q , p q r s , p r s q , p r q s , r p q s ] 800000... [r s p q , p q r s , p r s q , p r q s , r p q s ] 850000... [r s p q , p q r s , p r s q , p r q s , r p q s ] 900000... [r s p q , p q r s , p r s q , p r q s , r p q s ] 950000... [p q p q , r s p q , p q r s , p r s q , p r q s , r p q s ] done, results were: p q p q : 1 r s p q : 60499 p q r s : 939460 p r s q : 23 p r q s : 2 r p q s : 15 

i think have proved p q p q indeed possible, though @ extremely low probability of around one in million...

[edit: run, different results showing r s r s possible:]

done, results were:  r s r s : 1  r p s q : 2  p q p q : 1  r s p q : 445102  p q r s : 554877  p r s q : 5  p r q s : 2  r p q s : 10 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -