Add test for reentrant feature of the synchronized blocks
[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 synchronized void add(long value){
10     this.count += value;
11   }
12   
13   public synchronized long getCounter() {
14     return this.count;
15   }
16 }
17
18 public class CounterThread extends Thread{
19
20   String name;
21   protected Counter counter;
22
23   public CounterThread(String name, Counter counter){
24     this.name = name;
25     this.counter = counter;
26   }
27
28   public void run() {
29     for(int i=0; i<10; i++){
30       System.printString(this.name);
31       counter.add(i);
32       System.printString(" " + counter.getCounter() + "\n");
33     }
34   }
35 }
36
37 public class SynchonizedTest {
38   public SynchonizedTest() {
39   }
40
41   public static void main(String[] args){
42     Counter counter = new Counter();
43     Thread  threadA = new CounterThread("A\n",counter);
44     Thread  threadB = new CounterThread("B\n",counter);
45
46     threadA.start();
47     threadB.start(); 
48   }
49   
50   /*public static void main(String[] args){
51     Counter counterA = new Counter();
52     Counter counterB = new Counter();
53     Thread  threadA = new CounterThread("A\n",counterA);
54     Thread  threadB = new CounterThread("B\n",counterB);
55
56     threadA.start();
57     threadB.start(); 
58   }*/
59 }