KillThread.java revision 2362
9512fe850e98fdd448c638ca63fdd92a8a510255ahl/*
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9512fe850e98fdd448c638ca63fdd92a8a510255ahl *
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * This code is free software; you can redistribute it and/or modify it
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * under the terms of the GNU General Public License version 2 only, as
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * published by the Free Software Foundation.
9512fe850e98fdd448c638ca63fdd92a8a510255ahl *
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * This code is distributed in the hope that it will be useful, but WITHOUT
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * version 2 for more details (a copy is included in the LICENSE file that
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * accompanied this code).
9512fe850e98fdd448c638ca63fdd92a8a510255ahl *
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * You should have received a copy of the GNU General Public License version
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * 2 along with this work; if not, write to the Free Software Foundation,
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9512fe850e98fdd448c638ca63fdd92a8a510255ahl *
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * or visit www.oracle.com if you need additional information or have any
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * questions.
9512fe850e98fdd448c638ca63fdd92a8a510255ahl */
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl/*
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * @test
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * @bug 4288198
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * @summary Killing a Timer thread causes the Timer to fail silently on
9512fe850e98fdd448c638ca63fdd92a8a510255ahl * subsequent use.
9512fe850e98fdd448c638ca63fdd92a8a510255ahl */
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahlimport java.util.*;
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahlpublic class KillThread {
9512fe850e98fdd448c638ca63fdd92a8a510255ahl public static void main (String[] args) throws Exception {
9512fe850e98fdd448c638ca63fdd92a8a510255ahl Timer t = new Timer();
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl // Start a mean event that kills the timer thread
9512fe850e98fdd448c638ca63fdd92a8a510255ahl t.schedule(new TimerTask() {
9512fe850e98fdd448c638ca63fdd92a8a510255ahl public void run() {
9512fe850e98fdd448c638ca63fdd92a8a510255ahl throw new ThreadDeath();
9512fe850e98fdd448c638ca63fdd92a8a510255ahl }
9512fe850e98fdd448c638ca63fdd92a8a510255ahl }, 0);
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl // Wait for mean event to do the deed and thread to die.
9512fe850e98fdd448c638ca63fdd92a8a510255ahl try {
9512fe850e98fdd448c638ca63fdd92a8a510255ahl Thread.sleep(100);
9512fe850e98fdd448c638ca63fdd92a8a510255ahl } catch(InterruptedException e) {
9512fe850e98fdd448c638ca63fdd92a8a510255ahl }
9512fe850e98fdd448c638ca63fdd92a8a510255ahl
9512fe850e98fdd448c638ca63fdd92a8a510255ahl // Try to start another event
9512fe850e98fdd448c638ca63fdd92a8a510255ahl try {
9512fe850e98fdd448c638ca63fdd92a8a510255ahl // Timer thread is dead now
9512fe850e98fdd448c638ca63fdd92a8a510255ahl t.schedule(new TimerTask() {
9512fe850e98fdd448c638ca63fdd92a8a510255ahl public void run() {
9512fe850e98fdd448c638ca63fdd92a8a510255ahl }
9512fe850e98fdd448c638ca63fdd92a8a510255ahl }, 0);
9512fe850e98fdd448c638ca63fdd92a8a510255ahl throw new Exception("We failed silently");
9512fe850e98fdd448c638ca63fdd92a8a510255ahl } catch(IllegalStateException e) {
9512fe850e98fdd448c638ca63fdd92a8a510255ahl // Killing the Timer thread is equivalent to cancelling the Timer
9512fe850e98fdd448c638ca63fdd92a8a510255ahl }
9512fe850e98fdd448c638ca63fdd92a8a510255ahl }
9512fe850e98fdd448c638ca63fdd92a8a510255ahl}