Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/g1/unloading/keepref/InStackLocalHolder.java
41161 views
/*1* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package gc.g1.unloading.keepref;2324import gc.g1.unloading.check.cleanup.UnusedThreadKiller;2526/**27* This holder prevents class from being unloaded by keeping reference in stack local.28*29*/30public class InStackLocalHolder implements RefHolder {3132public static class AuxiliaryThread extends Thread {33private static final int STACK_DEPTH = 300;3435private ReferenceHolder referenceHolder;3637public AuxiliaryThread(ReferenceHolder referenceHolder) {38this.referenceHolder = referenceHolder;39}4041synchronized public void finishThread() {42notifyAll();43}4445@Override46synchronized public void run() {47new A().call(referenceHolder, STACK_DEPTH);48}49}5051private static class A {52public void call(ReferenceHolder holder, int stackDepth) {53if (stackDepth > 0) {54new A().call(holder, stackDepth - 1);55} else {56Object ref = holder.obtainAndClear();57synchronized (Thread.currentThread()) {58try {59Thread.currentThread().wait();60if (ref.hashCode() == 42) {61System.out.println("This clause is made to prevent compiler and javac optimizations from eliminating local reference \"ref\".");62}63} catch (InterruptedException e) {64new RuntimeException("Unexpected InterruptedException");65}66}67}68}69}7071private static class ReferenceHolder {72private Object reference;7374public ReferenceHolder(Object reference) {75this.reference = reference;76}7778public Object obtainAndClear() {79Object returnValue = reference;80reference = new Object();81return returnValue;82}8384}8586@Override87public Object hold(Object object) {88Thread thread = new AuxiliaryThread(new ReferenceHolder(object));89thread.setDaemon(true);90thread.start();91return new UnusedThreadKiller(thread.getId());92}9394}959697