Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Runtime/shutdown/ShutdownHooks.java
47311 views
/*1* Copyright (c) 2009, 2011, 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*/2223/*24* @bug 682950325* @summary 1) Test Console and DeleteOnExitHook can be initialized26* while shutdown is in progress27* 2) Test if files that are added by the application shutdown28* hook are deleted on exit during shutdown29*/30import java.io.*;31public class ShutdownHooks {32private static File file;33public static void main(String[] args) throws Exception {34if (args.length != 2) {35throw new IllegalArgumentException("Usage: ShutdownHooks <dir> <filename>");36}3738// Add a shutdown hook39Runtime.getRuntime().addShutdownHook(new Cleaner());4041File dir = new File(args[0]);42file = new File(dir, args[1]);43// write to file44System.out.println("writing to "+ file);45try (PrintWriter pw = new PrintWriter(file)) {46pw.println("Shutdown begins");47}48}4950public static class Cleaner extends Thread {51public void run() {52// register the Console's shutdown hook while the application53// shutdown hook is running54Console cons = System.console();55// register the DeleteOnExitHook while the application56// shutdown hook is running57file.deleteOnExit();58try (PrintWriter pw = new PrintWriter(file)) {59pw.println("file is being deleted");60} catch (FileNotFoundException e) {61throw new RuntimeException(e);62}63}64}6566}676869