-
Notifications
You must be signed in to change notification settings - Fork 81
/
ReservingStackAreas.java
64 lines (51 loc) · 1.54 KB
/
ReservingStackAreas.java
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
package dev.nipafx.demo.java9.runtime.stack;
// This import may lead to errors in your IDE - two choices:
// - put the import and the two uses below in comments
// - configure your IDE to add the following compiler argument:
// --add-exports=java.base/jdk.internal.vm.annotation=ALL-UNNAMED
import jdk.internal.vm.annotation.ReservedStackAccess;
public class ReservingStackAreas {
private static int depth = 0;
public static void main(String[] args) {
System.out.println("\nDEPTH & CALLS USING REGULAR STACK");
determineDepth();
recurseThenGreet();
System.out.println("\nDEPTH USING REGULAR STACK & CALLS USING RESERVED STACK");
determineDepth();
recurseWithReservedStackThenGreet();
System.out.println("\nDEPTH USING RESERVED STACK");
determineDepthWithReservedStack();
}
private static void determineDepth() {
depth = 0;
try {
recurseToDetermineMaxDepth();
} catch (StackOverflowError err) { }
System.out.printf("Depth: %d%n", depth);
}
private static void recurseToDetermineMaxDepth() {
depth++;
recurseToDetermineMaxDepth();
}
private static void recurseThenGreet() {
try {
recurseToDepth();
System.out.println("Hi!");
} catch (StackOverflowError err) {
System.out.println("Stack Overflow");
}
}
private static void recurseToDepth() {
depth--;
if (depth > 0)
recurseToDepth();
}
@ReservedStackAccess
private static void determineDepthWithReservedStack() {
determineDepth();
}
@ReservedStackAccess
private static void recurseWithReservedStackThenGreet() {
recurseThenGreet();
}
}