Explored trace needs to be constructed and modified as there are predecessor branches.
[jpf-core.git] / examples / ConcurrentCount.java
1 import java.util.concurrent.atomic.AtomicBoolean;
2
3 public class ConcurrentCount {
4         static final int COUNT = 30000;
5         volatile static int count = COUNT;
6         volatile static AtomicBoolean lock = new AtomicBoolean(false); 
7         static int a = 0;
8         static int b = 0;
9         
10         public static void main(String args[]) {
11                 
12                 new Thread() {
13                         
14                         @Override
15                         public void run() {
16                                 while(count > 0) {
17                                         if (lock.get()) continue;
18                                         lock.set(true);
19                                         decreaseCount();
20                                         a++;
21                                         lock.set(false);
22                                         
23                                         
24                                 }
25                         }
26                 }.start();
27                 
28         new Thread() {
29                         
30                         @Override
31                         public void run() {
32                                 while(count > 0) {
33                                         if (lock.get()) continue;
34                                         lock.set(true);
35                                         decreaseCount();
36                                         b++;
37                                         lock.set(false);
38                                         
39                                         
40                                 }
41                         }
42                 }.start();
43                 
44                 while(count > 0);
45                 //System.out.println("a = " + a);
46                 //System.out.println("b = " + b);
47                 //System.out.println("a + b = " + (a + b));
48                 //System.out.println("count = " + count);
49
50                 //Checks for concurrency error (which should be found when using model checking)
51                 assert a + b == COUNT;
52         }
53         
54         private static synchronized void decreaseCount() {
55                 count--;
56         }
57
58 }
59