Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/ServerSocket/ThreadStop.java
38811 views
/*1* Copyright (c) 2002, 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* @test25* @bug 468016026* @summary The deprecated Thread.stop exposes un-checked JNI calls27* that result in crashes when NULL is passed into subsequent28* JNI calls.29*/3031import java.net.*;32import java.io.IOException;3334public class ThreadStop {3536static class Server implements Runnable {3738ServerSocket ss;3940Server() throws IOException {41ss = new ServerSocket(0);42}4344public int localPort() {45return ss.getLocalPort();46}474849public void run() {50try {51Socket s = ss.accept();52} catch (IOException ioe) {53} catch (ThreadDeath x) {54} finally {55try {56ss.close();57} catch (IOException x) { }58}59}60}6162public static void main(String args[]) throws Exception {6364// start a server65Server svr = new Server();66Thread thr = new Thread(svr);67thr.start();6869// give server time to block in ServerSocket.accept()70Thread.currentThread().sleep(2000);7172// "stop" the thread73thr.stop();7475// give thread time to stop76Thread.currentThread().sleep(2000);7778// it's platform specific if Thread.stop interrupts the79// thread - on Linux/Windows most likely that thread is80// still in accept() so we connect to server which causes81// it to unblock and do JNI-stuff with a pending exception8283try {84Socket s = new Socket("localhost", svr.localPort());85} catch (IOException ioe) { }8687}8889}909192