Path: blob/master/test/jdk/java/io/BufferedReader/ReadLineSync.java
41149 views
/*1* Copyright (c) 2005, 2010, 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 507341426* @summary Ensure that there is no race condition in BufferedReader.readLine()27* when a line is terminated by '\r\n' is read by multiple threads.28*/2930import java.io.*;31import java.util.concurrent.TimeUnit;32import java.util.concurrent.ExecutorService;33import java.util.concurrent.Executors;3435public class ReadLineSync {3637public static int lineCount = 0;3839public static void main( String[] args ) throws Exception {4041String dir = System.getProperty(".", ".");42File f = new File(dir, "test.txt");43createFile(f);44f.deleteOnExit();4546BufferedReader reader = new BufferedReader(47new FileReader(f));48try {49int threadCount = 2;5051ExecutorService es = Executors.newFixedThreadPool(threadCount);5253for (int i=0; i < threadCount; i++)54es.execute(new BufferedReaderConsumer(reader));5556// Wait for the tasks to complete57es.shutdown();58while (!es.awaitTermination(60, TimeUnit.SECONDS));59} finally {60reader.close();61}62}6364static class BufferedReaderConsumer extends Thread {65BufferedReader reader;6667public BufferedReaderConsumer( BufferedReader reader ) {68this.reader = reader;69}7071public void run() {72try {73String record = reader.readLine();7475if ( record == null ) {76// if the first thread is too fast the second will hit77// this which is ok78System.out.println( "File already finished" );79return;80}8182if ( record.length() == 0 ) {83// usually it comes out here indicating the first read84// done by the second thread to run failed85System.out.println("Empty string on first read." +86Thread.currentThread().getName() );87}8889while ( record != null ) {90lineCount++;9192// Verify the token count93if ( record.length() == 0 ) {94// very occasionally it will fall over here95throw new Exception( "Invalid tokens with string '" +96record + "' on line " + lineCount );97}98record = reader.readLine();99}100}101catch ( Exception e ) {102e.printStackTrace();103}104}105}106107108// Create a relatively big file109110private static void createFile(File f) throws IOException {111BufferedWriter w = new BufferedWriter(112new FileWriter(f));113int count = 10000;114while (count > 0) {115116w.write("abcd \r\n");117w.write("efg \r\n");118w.write("hijk \r\n");119w.write("lmnop \r\n");120w.write("qrstuv \r\n");121w.write("wxy and z \r\n");122w.write("now you \r\n");123w.write("know your \r\n");124w.write("abc \r\n");125w.write("next time \r\n");126w.write("want you \r\n");127w.write("sing with me \r\n");128129count--;130}131w.close();132}133}134135136