summaryrefslogtreecommitdiffstats
path: root/test/109-suspend-check/src/Main.java
blob: 8046d751ed7bab83bb5bf84005c40db43b852c70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    private static final int TEST_TIME = 5;

    public static void main(String[] args) {
        System.out.println("Running (" + TEST_TIME + " seconds) ...");
        InfiniteDoWhileLoopWithLong doWhileLoopWithLong = new InfiniteDoWhileLoopWithLong();
        SimpleLoopThread[] simpleLoops = {
                new InfiniteForLoop(),
                new InfiniteWhileLoop(),
                new InfiniteWhileLoopWithIntrinsic(),
                new InfiniteDoWhileLoop(),
                new MakeGarbage(),
                new InfiniteWhileLoopWithSpecialReturnArgOrConst(new SpecialMethods1()),
                new InfiniteWhileLoopWithSpecialReturnArgOrConst(new SpecialMethods2()),
                new InfiniteWhileLoopWithSpecialPutOrNop(new SpecialMethods1()),
                new InfiniteWhileLoopWithSpecialPutOrNop(new SpecialMethods2()),
                new InfiniteWhileLoopWithSpecialConstOrIGet(new SpecialMethods1()),
                new InfiniteWhileLoopWithSpecialConstOrIGet(new SpecialMethods2()),
        };
        doWhileLoopWithLong.start();
        for (SimpleLoopThread loop : simpleLoops) {
            loop.start();
        }
        for (int i = 0; i < TEST_TIME; i++) {
          Runtime.getRuntime().gc();
          System.out.println(".");
          sleep(1000);
        }
        doWhileLoopWithLong.stopNow();
        for (SimpleLoopThread loop : simpleLoops) {
            loop.stopNow();
        }
        System.out.println("Done.");
    }

    public static void sleep(int ms) {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException ie) {
            System.err.println("sleep was interrupted");
        }
    }
}

class SimpleLoopThread extends Thread {
  volatile protected boolean keepGoing = true;
  public void stopNow() {
    keepGoing = false;
  }
}

interface SpecialMethodInterface {
  long ReturnArgOrConst(long arg);
  void PutOrNop(long arg);
  long ConstOrIGet();
}

class SpecialMethods1 implements SpecialMethodInterface {
  public long ReturnArgOrConst(long arg) {
    return 42L;
  }
  public void PutOrNop(long arg) {
  }
  public long ConstOrIGet() {
    return 42L;
  }
}

class SpecialMethods2 implements SpecialMethodInterface {
  public long value = 42L;
  public long ReturnArgOrConst(long arg) {
    return arg;
  }
  public void PutOrNop(long arg) {
    value = arg;
  }
  public long ConstOrIGet() {
    return value;
  }
}

class InfiniteWhileLoopWithSpecialReturnArgOrConst extends SimpleLoopThread {
  private SpecialMethodInterface smi;
  public InfiniteWhileLoopWithSpecialReturnArgOrConst(SpecialMethodInterface smi) {
    this.smi = smi;
  }
  public void run() {
    long i = 0L;
    while (keepGoing) {
      i += smi.ReturnArgOrConst(i);
    }
  }
}

class InfiniteWhileLoopWithSpecialPutOrNop extends SimpleLoopThread {
  private SpecialMethodInterface smi;
  public InfiniteWhileLoopWithSpecialPutOrNop(SpecialMethodInterface smi) {
    this.smi = smi;
  }
  public void run() {
    long i = 0L;
    while (keepGoing) {
      smi.PutOrNop(i);
      i++;
    }
  }
}

class InfiniteWhileLoopWithSpecialConstOrIGet extends SimpleLoopThread {
  private SpecialMethodInterface smi;
  public InfiniteWhileLoopWithSpecialConstOrIGet(SpecialMethodInterface smi) {
    this.smi = smi;
  }
  public void run() {
    long i = 0L;
    while (keepGoing) {
      i += smi.ConstOrIGet();
    }
  }
}

class InfiniteWhileLoopWithIntrinsic extends SimpleLoopThread {
  private String[] strings = { "a", "b", "c", "d" };
  private int sum = 0;
  public void run() {
    int i = 0;
    while (keepGoing) {
      i++;
      sum += strings[i & 3].length();
    }
  }
}

class InfiniteDoWhileLoopWithLong extends Thread {
  volatile private long keepGoing = 7L;
  public void run() {
    int i = 0;
    do {
      i++;
    } while (keepGoing >= 4L);
  }
  public void stopNow() {
    keepGoing = 1L;
  }
}

class InfiniteWhileLoop extends SimpleLoopThread {
  public void run() {
    int i = 0;
    while (keepGoing) {
      i++;
    }
  }
}

class InfiniteDoWhileLoop extends SimpleLoopThread {
  public void run() {
    int i = 0;
    do {
      i++;
    } while (keepGoing);
  }
}

class InfiniteForLoop extends SimpleLoopThread {
  public void run() {
    int i = 0;
    for (int j = 0; keepGoing; j++) {
      i += j;
    }
  }
}

class MakeGarbage extends SimpleLoopThread {
  public void run() {
    while (keepGoing) {
      byte[] garbage = new byte[100000];
    }
  }
}