Added test for PR131.
[cacao.git] / tests / gc / ThreadSleep.java
1 import java.util.Random;
2
3 public class ThreadSleep extends Thread {
4         public static final int N=5;
5         public static final int MOD_GC=2;
6         public static final int SLEEP_FIX=1000;
7         public static final int SLEEP_RND=500;
8
9         public static Random rnd;
10
11         public int id;
12         public int cnt;
13         public Object o;
14
15         public void run() {
16                 String myString;
17
18                 myString = new String("LocObj#" + this.id);
19
20                 while (true) {
21                         try {
22                                 sleep(SLEEP_FIX + rnd.nextInt(SLEEP_RND));
23                         } catch(Exception e) {
24                                 System.out.println("Thread #" + this.id + " had an Exception");
25                         }
26
27                         this.cnt++;
28
29                         System.out.println("(" + this.id + ") Thread woke up:");
30                         System.out.println("(" + this.id + ")\t cnt=" + this.cnt + ", o=\'" + this.o + "\'");
31                         System.out.println("(" + this.id + ")\t local=\'" + myString + "\'");
32
33                         if (this.cnt % MOD_GC == 0) {
34                                 System.out.println("(" + this.id + ") Starting the GC now!...");
35                                 System.gc();
36                         }
37                 }
38         }
39
40         public static void main(String args[]) {
41                 ThreadSleep t = null;
42
43                 rnd = new Random();
44
45                 System.out.println("Creating and starting threads ...");
46                 for (int i=0; i<N; i++) {
47                         t = new ThreadSleep();
48                         t.id = i;
49                         t.cnt = 0;
50                         t.o = new String("GlobObj#" + i);
51                         t.start();
52                 }
53                 t = null;
54
55                 System.out.println("Finished.");
56         }
57 }