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