0N/A/*
3261N/A * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 5073414
0N/A * @summary Ensure that there is no race condition in BufferedReader.readLine()
0N/A * when a line is terminated by '\r\n' is read by multiple threads.
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.concurrent.TimeUnit;
0N/Aimport java.util.concurrent.ExecutorService;
0N/Aimport java.util.concurrent.Executors;
0N/A
0N/Apublic class ReadLineSync {
0N/A
0N/A public static int lineCount = 0;
0N/A
0N/A public static void main( String[] args ) throws Exception {
0N/A
0N/A String dir = System.getProperty(".", ".");
0N/A File f = new File(dir, "test.txt");
0N/A createFile(f);
0N/A f.deleteOnExit();
0N/A
0N/A BufferedReader reader = new BufferedReader(
0N/A new FileReader(f));
2497N/A try {
2497N/A int threadCount = 2;
0N/A
2497N/A ExecutorService es = Executors.newFixedThreadPool(threadCount);
0N/A
2497N/A for (int i=0; i < threadCount; i++)
2497N/A es.execute(new BufferedReaderConsumer(reader));
0N/A
2497N/A // Wait for the tasks to complete
2497N/A es.shutdown();
2497N/A while (!es.awaitTermination(60, TimeUnit.SECONDS));
2497N/A } finally {
2497N/A reader.close();
2497N/A }
0N/A }
0N/A
0N/A static class BufferedReaderConsumer extends Thread {
0N/A BufferedReader reader;
0N/A
0N/A public BufferedReaderConsumer( BufferedReader reader ) {
0N/A this.reader = reader;
0N/A }
0N/A
0N/A public void run() {
0N/A try {
0N/A String record = reader.readLine();
0N/A
0N/A if ( record == null ) {
0N/A // if the first thread is too fast the second will hit
0N/A // this which is ok
0N/A System.out.println( "File already finished" );
0N/A return;
0N/A }
0N/A
0N/A if ( record.length() == 0 ) {
0N/A // usually it comes out here indicating the first read
0N/A // done by the second thread to run failed
0N/A System.out.println("Empty string on first read." +
0N/A Thread.currentThread().getName() );
0N/A }
0N/A
0N/A while ( record != null ) {
0N/A lineCount++;
0N/A
0N/A // Verify the token count
0N/A if ( record.length() == 0 ) {
0N/A // very occasionally it will fall over here
0N/A throw new Exception( "Invalid tokens with string '" +
0N/A record + "' on line " + lineCount );
0N/A }
0N/A record = reader.readLine();
0N/A }
0N/A }
0N/A catch ( Exception e ) {
0N/A e.printStackTrace();
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A // Create a relatively big file
0N/A
0N/A private static void createFile(File f) throws IOException {
0N/A BufferedWriter w = new BufferedWriter(
0N/A new FileWriter(f));
0N/A int count = 10000;
0N/A while (count > 0) {
0N/A
0N/A w.write("abcd \r\n");
0N/A w.write("efg \r\n");
0N/A w.write("hijk \r\n");
0N/A w.write("lmnop \r\n");
0N/A w.write("qrstuv \r\n");
0N/A w.write("wxy and z \r\n");
0N/A w.write("now you \r\n");
0N/A w.write("know your \r\n");
0N/A w.write("abc \r\n");
0N/A w.write("next time \r\n");
0N/A w.write("want you \r\n");
0N/A w.write("sing with me \r\n");
0N/A
0N/A count--;
0N/A }
0N/A w.close();
0N/A }
0N/A}