Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Runtime/exec/ConcurrentRead.java
47209 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 461974426* @summary Test that Process input/out can be concurrently read/written27* @author kladko28*/2930import java.io.InputStream;31import java.io.OutputStream;32import java.io.File;33import java.io.IOException;3435public class ConcurrentRead {3637static volatile Exception savedException;38static final String TEE = "/usr/bin/tee";3940public static void main(String[] args) throws Exception {4142if (File.separatorChar == '\\' || // Windows43!new File(TEE).exists()) // no tee44return;4546Process p = Runtime.getRuntime().exec(TEE);47OutputStream out = p.getOutputStream();48InputStream in = p.getInputStream();49Thread t1 = new WriterThread(out, in);50t1.start();51Thread t2 = new WriterThread(out, in);52t2.start();53t1.join();54t2.join();55if (savedException != null)56throw savedException;57}5859static class WriterThread extends Thread {60OutputStream out;61InputStream in;62WriterThread(OutputStream out, InputStream in) {63this.out = out;64this.in = in;65}66public void run(){67try {68out.write('a');69out.flush();70if (in.read() == -1) // got end-of-stream71throw new Exception("End of stream in writer thread");72} catch (Exception e) {73savedException = e;74}75}76}77}787980