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