* configure.ac: New switch for disabling -O2 (--disable-optimizations).
[cacao.git] / tests / gc / LockRecords.java
1 import java.util.Vector;
2
3 public class LockRecords {
4         static final int N = 3;
5
6         static Vector v;
7
8         static class MyObject
9         {
10                 public int id;
11                 public int counter;
12
13                 protected void finalize() throws Throwable
14                 {
15                         System.out.println("\tObject #" + id + " gets finalized");
16                         throw new Exception("Object #" + id + " is nasty!");
17                 }
18         };
19
20         static class MyThread extends Thread
21         {
22                 public String name;
23                 public synchronized void run()
24                 {
25                         System.out.println("\t" + name + ": Starting ...");
26                         try {
27                                 for (int i=0; i<N; i++) {
28                                         MyObject o = (MyObject) v.get(i);
29                                         System.out.println("\t" + name + ": Trying on #" + o.id);
30                                         synchronized(o) {
31                                                 System.out.println("\t" + name + ": Locked on #" + o.id);
32                                                 this.wait(200);
33                                                 System.out.println("\t" + name + ": Releasing #" + o.id);
34                                                 o.counter--;
35                                         }
36                                         while (o.counter > 0) {
37                                                 this.wait(10);
38                                         }
39                                 }
40                         } catch (Exception e) {
41                                 e.printStackTrace();
42                         }
43                         System.out.println("\t" + name + ": finished.");
44                 }
45         };
46
47         public synchronized void test()
48         {
49                 System.out.println("Creating Objects ...");
50                 v = new Vector();
51                 for (int i=0; i<N; i++) {
52                         MyObject o = new MyObject();
53                         o.id = i;
54                         o.counter = 2;
55                         v.add(o);
56                 }
57
58                 System.out.println("Starting Blocking Threads A + B...");
59                 MyThread threadA = new MyThread();
60                 MyThread threadB = new MyThread();
61                 threadA.name = "A"; threadA.start();
62                 threadB.name = "B"; threadB.start();
63                 try {
64                         threadA.join();
65                         threadB.join();
66                 } catch (Exception e) {
67                         e.printStackTrace();
68                 }
69
70                 System.out.println("Cleaning up ...");
71                 v = null;
72                 System.gc();
73
74                 System.out.println("Waiting some seconds ...");
75                 try {
76                         wait(3000);
77                 } catch (Exception e) {
78                         e.printStackTrace();
79                 }
80
81                 System.out.println("Cleaning up again ...");
82                 System.gc();
83         }
84
85         public static void main(String args[])
86         {
87                 LockRecords test = new LockRecords();
88                 test.test();
89         }
90 }