Path: blob/master/test/jdk/java/io/PipedInputStream/WriterLoop.java
41149 views
/*1* Copyright (c) 2005, 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 621975526* @summary Write end loops infinitely when the27* buffer is full and the read end has closed.28*/2930import java.io.*;3132public class WriterLoop extends Thread {3334static PipedOutputStream out;35static PipedInputStream in;3637public void run() {38try {39System.out.println("Writer started.");4041// without the fix, this test will hang at this point,42// i.e inside the call to write()43out.write(new byte[64*1024]);44} catch (Throwable e) {4546// with the fix an IOException is caught47System.out.println("Writer exception:");48e.printStackTrace();49} finally {50System.out.println("Writer done.");51}52}5354public static void main(String args[]) throws Exception {55in = new PipedInputStream();56out = new PipedOutputStream(in);57WriterLoop writer = new WriterLoop();58writer.start();5960try {61System.out.println("Reader reading...");62in.read(new byte[2048]);6364System.out.println("Reader closing stream...");65in.close();6667System.out.println("Reader sleeping 3 seconds...");68Thread.sleep(3000);69} catch (Throwable e) {70System.out.println("Reader exception:");71e.printStackTrace();72} finally {73System.out.println("Active threads:");74Thread[] threads = new Thread[Thread.activeCount()];75Thread.enumerate(threads);76for (int i = 0; i < threads.length; i++) {77System.out.println(" " + threads[i]);78}79System.out.println("Waiting for writer...");80writer.join();81System.out.println("Done.");82}83}84}858687