Path: blob/master/test/jdk/java/lang/RuntimeTests/exec/ConcurrentRead.java
41153 views
/*1* Copyright (c) 2002, 2019, 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 461974426* @summary Test that Process input/out can be concurrently read/written27* @author kladko28*/2930import java.io.InputStream;31import java.io.OutputStream;3233public class ConcurrentRead {3435static volatile Exception savedException;3637public static void main(String[] args) throws Exception {38if (! UnixCommands.isUnix) {39System.out.println("For UNIX only");40return;41}42UnixCommands.ensureCommandsAvailable("tee");4344Process p = Runtime.getRuntime().exec(UnixCommands.tee());45OutputStream out = p.getOutputStream();46InputStream in = p.getInputStream();47Thread t1 = new WriterThread(out, in);48t1.start();49Thread t2 = new WriterThread(out, in);50t2.start();51t1.join();52t2.join();53if (savedException != null)54throw savedException;55}5657static class WriterThread extends Thread {58OutputStream out;59InputStream in;60WriterThread(OutputStream out, InputStream in) {61this.out = out;62this.in = in;63}64public void run(){65try {66out.write('a');67out.flush();68if (in.read() == -1) // got end-of-stream69throw new Exception("End of stream in writer thread");70} catch (Exception e) {71savedException = e;72}73}74}75}767778