Path: blob/master/test/jdk/java/nio/channels/FileChannel/InterruptMapDeadlock.java
41154 views
/*1* Copyright (c) 2013, 2016, 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* @key intermittent26* @summary Tests interruption of threads mapping sections of a file channel in27* an attempt to deadlock due to nesting of begin calls.28*/29import java.io.IOException;30import java.nio.ByteBuffer;31import java.nio.channels.*;32import java.nio.channels.FileChannel.MapMode;33import java.nio.file.*;34import java.util.concurrent.Semaphore;35import static java.nio.file.StandardOpenOption.*;3637public class InterruptMapDeadlock {3839static class Mapper extends Thread {40final FileChannel fc;41final Semaphore gate;42volatile Exception exception;4344Mapper(FileChannel fc, Semaphore gate) {45this.fc = fc;46this.gate = gate;47}4849@Override50public void run() {51try {52gate.acquireUninterruptibly();53fc.map(MapMode.READ_ONLY, 0, 1);54throw new Exception("Map succeeded");55} catch (IOException x) {56System.out.println(x.getClass() + " (expected)");57} catch (Exception unexpected) {58this.exception = unexpected;59}60}6162Exception exception() {63return exception;64}6566static Mapper startMapper(FileChannel fc, Semaphore gate) {67Mapper r = new Mapper(fc, gate);68r.setDaemon(true);69r.start();70return r;71}72}7374static class Interruptor extends Thread {7576final Mapper[] mappers;77final Semaphore gate;7879Interruptor(Mapper[] mappers, Semaphore gate) {80this.mappers = mappers;81this.gate = gate;82}8384public void run() {85gate.release(mappers.length);86for (Mapper m : mappers) {87m.interrupt();88}89}90}91// the number of mapper threads to start92private static final int MAPPER_COUNT = 4;9394public static void main(String[] args) throws Exception {95Path file = Paths.get("data.txt");96FileChannel.open(file, CREATE, TRUNCATE_EXISTING, WRITE).close();9798Mapper[] mappers = new Mapper[MAPPER_COUNT];99100for (int i=1; i<=20; i++) {101System.out.format("Iteration: %s%n", i);102103FileChannel fc = FileChannel.open(file);104boolean failed = false;105106Semaphore gate = new Semaphore(0);107// start mapper threads108for (int j=0; j<MAPPER_COUNT; j++) {109mappers[j] = Mapper.startMapper(fc, gate);110}111112// interrupt and wait for the mappers to terminate113Interruptor interruptor = new Interruptor(mappers, gate);114interruptor.start();115try {116interruptor.join(10000);117if (interruptor.isAlive()) {118System.err.println("Interruptor thread did not terminate:");119Throwable t = new Exception("Stack trace");120t.setStackTrace(interruptor.getStackTrace());121t.printStackTrace();122failed = true;123}124} catch (InterruptedException x) {125System.err.println("Main thread was interrupted");126failed = true;127}128129for (Mapper m: mappers) {130try {131m.join(10000);132Exception e = m.exception();133if (e != null) {134System.err.println("Mapper thread failed with: " + e);135failed = true;136} else if (m.isAlive()) {137System.err.println("Mapper thread did not terminate:");138Throwable t = new Exception("Stack trace");139t.setStackTrace(m.getStackTrace());140t.printStackTrace();141failed = true;142}143} catch (InterruptedException x) {144System.err.println("Main thread was interrupted");145failed = true;146}147}148149if (failed)150throw new RuntimeException("Test failed - see log for details");151else152fc.close();153}154}155}156157158