duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame^] | 2 | * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame^] | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | * or visit www.oracle.com if you need additional information or have any |
| 21 | * questions. |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @bug 5073414 |
| 27 | * @summary Ensure that there is no race condition in BufferedReader.readLine() |
| 28 | * when a line is terminated by '\r\n' is read by multiple threads. |
| 29 | */ |
| 30 | |
| 31 | import java.io.*; |
| 32 | import java.util.concurrent.TimeUnit; |
| 33 | import java.util.concurrent.ExecutorService; |
| 34 | import java.util.concurrent.Executors; |
| 35 | |
| 36 | public class ReadLineSync { |
| 37 | |
| 38 | public static int lineCount = 0; |
| 39 | |
| 40 | public static void main( String[] args ) throws Exception { |
| 41 | |
| 42 | String dir = System.getProperty(".", "."); |
| 43 | File f = new File(dir, "test.txt"); |
| 44 | createFile(f); |
| 45 | f.deleteOnExit(); |
| 46 | |
| 47 | BufferedReader reader = new BufferedReader( |
| 48 | new FileReader(f)); |
| 49 | int threadCount = 2; |
| 50 | |
| 51 | ExecutorService es = Executors.newFixedThreadPool(threadCount); |
| 52 | |
| 53 | for (int i=0; i < threadCount; i++) |
| 54 | es.execute(new BufferedReaderConsumer(reader)); |
| 55 | |
| 56 | // Wait for the tasks to complete |
| 57 | es.shutdown(); |
| 58 | while (!es.awaitTermination(60, TimeUnit.SECONDS)); |
| 59 | } |
| 60 | |
| 61 | static class BufferedReaderConsumer extends Thread { |
| 62 | BufferedReader reader; |
| 63 | |
| 64 | public BufferedReaderConsumer( BufferedReader reader ) { |
| 65 | this.reader = reader; |
| 66 | } |
| 67 | |
| 68 | public void run() { |
| 69 | try { |
| 70 | String record = reader.readLine(); |
| 71 | |
| 72 | if ( record == null ) { |
| 73 | // if the first thread is too fast the second will hit |
| 74 | // this which is ok |
| 75 | System.out.println( "File already finished" ); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | if ( record.length() == 0 ) { |
| 80 | // usually it comes out here indicating the first read |
| 81 | // done by the second thread to run failed |
| 82 | System.out.println("Empty string on first read." + |
| 83 | Thread.currentThread().getName() ); |
| 84 | } |
| 85 | |
| 86 | while ( record != null ) { |
| 87 | lineCount++; |
| 88 | |
| 89 | // Verify the token count |
| 90 | if ( record.length() == 0 ) { |
| 91 | // very occasionally it will fall over here |
| 92 | throw new Exception( "Invalid tokens with string '" + |
| 93 | record + "' on line " + lineCount ); |
| 94 | } |
| 95 | record = reader.readLine(); |
| 96 | } |
| 97 | } |
| 98 | catch ( Exception e ) { |
| 99 | e.printStackTrace(); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | |
| 105 | // Create a relatively big file |
| 106 | |
| 107 | private static void createFile(File f) throws IOException { |
| 108 | BufferedWriter w = new BufferedWriter( |
| 109 | new FileWriter(f)); |
| 110 | int count = 10000; |
| 111 | while (count > 0) { |
| 112 | |
| 113 | w.write("abcd \r\n"); |
| 114 | w.write("efg \r\n"); |
| 115 | w.write("hijk \r\n"); |
| 116 | w.write("lmnop \r\n"); |
| 117 | w.write("qrstuv \r\n"); |
| 118 | w.write("wxy and z \r\n"); |
| 119 | w.write("now you \r\n"); |
| 120 | w.write("know your \r\n"); |
| 121 | w.write("abc \r\n"); |
| 122 | w.write("next time \r\n"); |
| 123 | w.write("want you \r\n"); |
| 124 | w.write("sing with me \r\n"); |
| 125 | |
| 126 | count--; |
| 127 | } |
| 128 | w.close(); |
| 129 | } |
| 130 | } |