summaryrefslogtreecommitdiffstats
path: root/test/067-preemptive-unpark/src/Main.java
blob: 2c099b946e523f0ba6b8044edb06429ad753c323 (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
import sun.misc.Unsafe;

import java.lang.reflect.Field;

public class Main {
    private static Unsafe UNSAFE;

    public static void main(String[] args) throws Exception {
        setUp();

        ParkTester test = new ParkTester();

        System.out.println("Test starting");

        test.start();
        UNSAFE.unpark(test);
        clearStack(10);

        System.out.println("GC'ing");
        System.gc();
        System.runFinalization();
        System.gc();

        System.out.println("Asking thread to park");
        test.parkNow = true;

        try {
            Thread.sleep(1500);
        } catch (InterruptedException ex) {
            // Ignore it.
        }

        if (test.success) {
            System.out.println("Test succeeded!");
        } else {
            System.out.println("Test failed.");
        }
    }

    /**
     * Set up {@link #UNSAFE}.
     */
    public static void setUp() {
        /*
         * Subvert the access check to get the unique Unsafe instance.
         * We can do this because there's no security manager
         * installed when running the test.
         */
        try {
            Field field = Unsafe.class.getDeclaredField("THE_ONE");
            field.setAccessible(true);

            UNSAFE = (Unsafe) field.get(null);
        } catch (NoSuchFieldException ex) {
            throw new RuntimeException(ex);
        } catch (IllegalAccessException ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * Scribbles on the stack to help ensure we don't have a fake
     * pointer that would keep would-be garbage alive.
     */
    private static void clearStack(int depth) {
        int a = 0;
        int b = 0;
        int c = 0;
        int d = 0;
        int e = 0;
        int f = 0;
        int g = 0;
        int h = 0;
        int i = 0;
        int j = 0;

        if (depth > 0) {
            clearStack(depth - 1);
        }
    }

    private static class ParkTester extends Thread {
        public volatile boolean parkNow = false;
        public volatile boolean success = false;

        public void run() {
            while (!parkNow) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ex) {
                    // Ignore it.
                }
            }

            long start = System.currentTimeMillis();
            UNSAFE.park(false, 500 * 1000000); // 500 msec
            long elapsed = System.currentTimeMillis() - start;

            if (elapsed > 200) {
                System.out.println("park()ed for " + elapsed + " msec");
                success = false;
            } else {
                System.out.println("park() returned quickly");
                success = true;
            }
        }
    }
}