hotspot/test/runtime/Thread/Fibonacci.java
author ctornqvi
Thu, 26 Feb 2015 06:11:56 -0800
changeset 29318 506962bddb02
child 30604 b8d532cb6420
permissions -rw-r--r--
8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread Reviewed-by: dholmes, ctornqvi Contributed-by: dmitry.dmitriev@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29318
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
     1
/*
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
     4
 *
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
     7
 * published by the Free Software Foundation.
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
     8
 *
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    13
 * accompanied this code).
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    14
 *
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    18
 *
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    21
 * questions.
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    22
 */
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    23
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    24
/**
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    25
 * @test
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    26
 * @summary Calculates Fibonacci numbers "recursively" via threads and compares
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    27
 *     the result with the classical calculation.
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    28
 *     This test is skipped on 32-bit Windows: limited virtual space on Win-32
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    29
 *     make this test inherently unstable on Windows with 32-bit VM data model.
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    30
 * @requires !(os.family == "windows" & sun.arch.data.model == "32")
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    31
 * @library /testlibrary
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    32
 * @run main Fibonacci 15
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    33
 */
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    34
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    35
import com.oracle.java.testlibrary.Asserts;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    36
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    37
public class Fibonacci extends Thread {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    38
    private int index;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    39
    private int value;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    40
    private Fibonacci left;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    41
    private Fibonacci right;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    42
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    43
    public Fibonacci(int i) {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    44
        index = i;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    45
    }
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    46
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    47
    private int getValue() {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    48
        return value;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    49
    }
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    50
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    51
    @Override
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    52
    public void run() {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    53
        if (index == 0 || index == 1) {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    54
            // base cases, 0 Fibonacci number = 0, 1 Fibonacci number = 1
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    55
            value = index;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    56
        } else {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    57
            // inductive cases
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    58
            left = new Fibonacci(index - 2);
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    59
            right = new Fibonacci(index - 1);
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    60
            left.start();
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    61
            right.start();
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    62
            try {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    63
                left.join();
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    64
                right.join();
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    65
            } catch (InterruptedException e) {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    66
                throw new Error("InterruptedException for index " + index, e);
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    67
            }
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    68
            // compute and terminate
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    69
            value = left.getValue() + right.getValue();
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    70
        }
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    71
    }
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    72
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    73
    public static int traditionalFibonacci(int n) {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    74
        int n1 = 0, n2 = 1, nn = 0;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    75
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    76
        if (n == 0 || n == 1) {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    77
           nn = n;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    78
        }
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    79
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    80
        for (int i = 1; i < n; ++i) {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    81
            nn = n2 + n1;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    82
            n1 = n2;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    83
            n2 = nn;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    84
        }
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    85
        return nn;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    86
    }
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    87
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    88
    public static void main(String[] args) throws Error,AssertionError {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    89
        int expected;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    90
        int number;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    91
        Fibonacci recursiveFibonacci;
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    92
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    93
        if (args.length != 1) {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    94
            throw new Error("Error: args.length must be 1");
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    95
        }
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    96
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    97
        number = Integer.parseInt(args[0]);
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    98
        recursiveFibonacci = new Fibonacci(number);
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
    99
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   100
        recursiveFibonacci.start();
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   101
        try {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   102
            recursiveFibonacci.join();
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   103
        } catch (InterruptedException e) {
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   104
            throw new Error("InterruptedException in main thread", e);
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   105
        }
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   106
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   107
        expected = traditionalFibonacci(number);
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   108
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   109
        System.out.println("Fibonacci[" + number + "] = " + expected);
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   110
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   111
        Asserts.assertEQ(recursiveFibonacci.getValue(), expected,
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   112
                          "Unexpected calculated value: " + recursiveFibonacci.getValue() + " expected " + expected );
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   113
    }
506962bddb02 8069291: [TESTBUG] runtime/threads/Fibonacci: OutOfMemoryError: unable to create native thread
ctornqvi
parents:
diff changeset
   114
}