Fix bug for synchronized blocks. Inside a synchronized block, there could have return...
[IRC.git] / Robust / src / Tests / MGC / SynchonizedTest.java
1 public class Counter{
2      
3   long count;
4   
5   public Counter() {
6     this.count = 0;
7   }
8
9   public add(long value){
10         synchronized (Counter.class) {
11       this.count += value;
12         }
13   }
14   
15   public synchronized long getCounter() {
16     return this.count;
17   }
18 }
19
20 public class CounterThread extends Thread{
21
22   String name;
23   protected Counter counter;
24
25   public CounterThread(String name, Counter counter){
26     this.name = name;
27     this.counter = counter;
28   }
29
30   public void run() {
31     for(int i=0; i<10; i++){
32       System.printString(this.name);
33       counter.add(i);
34       System.printString(" " + counter.getCounter() + "\n");
35     }
36   }
37 }
38
39 public class CounterS{
40   
41   static long count = 1;
42
43   public CounterS() {
44   }
45
46   public long add(long value){
47     synchronized (CounterS.class) {
48       CounterS.count += value;
49       return CounterS.count;
50     }
51   }
52   
53   public synchronized static long getCounter() {
54     return CounterS.count;
55   }
56 }
57
58 public class CounterSThread extends Thread{
59
60   String name;
61   protected CounterS counter;
62
63   public CounterSThread(String name, CounterS counter){
64     this.name = name;
65     this.counter = counter;
66   }
67
68   public void run() {
69     for(int i=0; i<10; i++){
70       System.printString(this.name);
71       counter.add(i);
72       System.printString("  " + counter.getCounter() + "\n");
73     }
74   }
75 }
76
77 public class SynchonizedTest {
78   public SynchonizedTest() {
79   }
80
81   public static void main(String[] args){
82     Counter counter = new Counter();
83     Thread  threadA = new CounterThread("A\n",counter);
84     Thread  threadB = new CounterThread("B\n",counter);
85
86     threadA.start();
87     threadB.start(); 
88
89     CounterS countersA = new CounterS();
90     CounterS countersB = new CounterS();
91     Thread  threadsA = new CounterSThread("C\n",countersA);
92     Thread  threadsB = new CounterSThread("D\n",countersB);
93
94     threadsA.start();
95     threadsB.start(); 
96   }
97 }