Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/FileChannel/InterruptMapDeadlock.java
38828 views
/*1* Copyright (c) 2013, 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/* @test24* @bug 802483325* @summary Tests interruption of threads mapping sections of a file channel in26* an attempt to deadlock due to nesting of begin calls.27*/28import java.io.IOException;29import java.nio.ByteBuffer;30import java.nio.channels.*;31import java.nio.channels.FileChannel.MapMode;32import java.nio.file.*;33import java.util.concurrent.Semaphore;34import static java.nio.file.StandardOpenOption.*;3536public class InterruptMapDeadlock {3738static class Mapper extends Thread {39final FileChannel fc;40final Semaphore gate;41volatile Exception exception;4243Mapper(FileChannel fc, Semaphore gate) {44this.fc = fc;45this.gate = gate;46}4748@Override49public void run() {50try {51gate.acquireUninterruptibly();52fc.map(MapMode.READ_ONLY, 0, 1);53throw new Exception("Map succeeded");54} catch (IOException x) {55System.out.println(x.getClass() + " (expected)");56} catch (Exception unexpected) {57this.exception = unexpected;58}59}6061Exception exception() {62return exception;63}6465static Mapper startMapper(FileChannel fc, Semaphore gate) {66Mapper r = new Mapper(fc, gate);67r.setDaemon(true);68r.start();69return r;70}71}7273static class Interruptor extends Thread {7475final Mapper[] mappers;76final Semaphore gate;7778Interruptor(Mapper[] mappers, Semaphore gate) {79this.mappers = mappers;80this.gate = gate;81}8283public void run() {84gate.release(mappers.length);85for (Mapper m : mappers) {86m.interrupt();87}88}89}90// the number of mapper threads to start91private static final int MAPPER_COUNT = 4;9293public static void main(String[] args) throws Exception {94Path file = Paths.get("data.txt");95FileChannel.open(file, CREATE, TRUNCATE_EXISTING, WRITE).close();9697Mapper[] mappers = new Mapper[MAPPER_COUNT];9899for (int i=1; i<=20; i++) {100System.out.format("Iteration: %s%n", i);101102FileChannel fc = FileChannel.open(file);103boolean failed = false;104105Semaphore gate = new Semaphore(0);106// start mapper threads107for (int j=0; j<MAPPER_COUNT; j++) {108mappers[j] = Mapper.startMapper(fc, gate);109}110111// interrupt and wait for the mappers to terminate112Interruptor interruptor = new Interruptor(mappers, gate);113interruptor.start();114try {115interruptor.join(10000);116if (interruptor.isAlive()) {117System.err.println("Interruptor thread did not terminate:");118Throwable t = new Exception("Stack trace");119t.setStackTrace(interruptor.getStackTrace());120t.printStackTrace();121failed = true;122}123} catch (InterruptedException x) {124System.err.println("Main thread was interrupted");125failed = true;126}127128for (Mapper m: mappers) {129try {130m.join(10000);131Exception e = m.exception();132if (e != null) {133System.err.println("Mapper thread failed with: " + e);134failed = true;135} else if (m.isAlive()) {136System.err.println("Mapper thread did not terminate:");137Throwable t = new Exception("Stack trace");138t.setStackTrace(m.getStackTrace());139t.printStackTrace();140failed = true;141}142} catch (InterruptedException x) {143System.err.println("Main thread was interrupted");144failed = true;145}146}147148if (failed)149throw new RuntimeException("Test failed - see log for details");150else151fc.close();152}153}154}155156157