1 /* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.internal.ref; 27 28 import dalvik.system.ZygoteHooks; 29 30 import jdk.internal.misc.InnocuousThread; 31 32 import java.lang.ref.Cleaner; 33 import java.security.AccessController; 34 import java.security.PrivilegedAction; 35 import java.util.concurrent.ThreadFactory; 36 37 /** 38 * CleanerFactory provides a Cleaner for use within system modules. 39 * The cleaner is created on the first reference to the CleanerFactory. 40 */ 41 public final class CleanerFactory { 42 43 /* The common Cleaner. */ 44 private final static Cleaner commonCleaner = Cleaner.create(new ThreadFactory() { 45 @Override 46 public Thread newThread(Runnable r) { 47 // Android-added: Fail immediately if we try this in the zygote, where we are 48 // not allowed to create additional threads. 49 if (ZygoteHooks.inZygote()) { 50 throw new AssertionError("Erroneously trying to create Cleaner in zygote"); 51 } 52 return AccessController.doPrivileged(new PrivilegedAction<>() { 53 @Override 54 public Thread run() { 55 Thread t = InnocuousThread.newSystemThread("Common-Cleaner", r); 56 t.setPriority(Thread.MAX_PRIORITY - 2); 57 return t; 58 } 59 }); 60 } 61 }); 62 63 /** 64 * Cleaner for use within system modules. 65 * 66 * This Cleaner will run on a thread whose context class loader 67 * is {@code null}. The system cleaning action to perform in 68 * this Cleaner should handle a {@code null} context class loader. 69 * 70 * @return a Cleaner for use within system modules 71 */ cleaner()72 public static Cleaner cleaner() { 73 return commonCleaner; 74 } 75 } 76