Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6354211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:32:27+00:00 2026-05-24T22:32:27+00:00

Here is a program that attempts to read 2 sentences from a file. For

  • 0

Here is a program that attempts to read 2 sentences from a file. For some reason the program attempts to read from a new thread . Before starting a new thread i initialize the BufferedReader object and pass it as a reference to a function that initiates a thread. Now i have 2 references of the BufferedReader object and i try to read the sentences from the main thread and the new thread created.

In the file the 2 sentences on new lines are :

    D:\UnderTest\wavtester_1.wav
    D:\UnderTest\wavtester_2.wav

But the output is surprising:

    FROM THE MAIN THREAD D:\UnderTest\wavtester_1.wav
    ON A NEW THREAD D:\UnderTest\wavtester_2.wav
    FROM THE MAIN THREAD

If we notice, the new thread does not read the first sentence and the first sentence has been read by the main thread. Also the 3rd output prints white space in front of FROM THE MAIN THREAD when the 2nd statement has been read by the new thread. So, should i interpret that the 2 references are actually one ?

If i am wrong then what actually is happening ?

import java.io.*;

class tester {
   public static void main( String args[] ) {
      try {
         BufferedReader bfr = new BufferedReader( new FileReader( new File( "e:\\AVS Player\\AudioPlaylists\\tester.txt") ) );
         // pass a reference of bfr and start a new thread
         startThread( bfr );
         String y = null;
         while( ( y = bfr.readLine() ) != null ) {
            System.out.println("FROM THE MAIN THREAD" + " " + y );
         }    
      } catch( Exception exc ) {
         System.out.println( exc );
      } 
   }   

   private static void startThread( final BufferedReader bfr ) {
      Runnable r = new Runnable() {
         @Override
         public void run() {
            fnctn( bfr );
         }
      };
      new Thread(r).start();
   }

   private static void fnctn( BufferedReader bfr ) {
      String x = null;
      try {
         //BufferedReader bfr1 = new BufferedReader( new FileReader( new File( "e:\\AVS Player\\AudioPlaylists\\tester.txt") ) );
         while( ( x = bfr.readLine() ) != null ) {
            System.out.println("ON A NEW THREAD" + " " + x);
         }
      } catch( Exception exc ) {
         System.out.println( exc );
      }
   }
}

As against this if in the method fnctn i initialize a new BufferedReader object and read from that reference the output is :

    FROM THE MAIN THREAD D:\UnderTest\wavtester_1.wav
    FROM THE MAIN THREAD D:\UnderTest\wavtester_2.wav
    FROM THE MAIN THREAD
    ON A NEW THREAD D:\UnderTest\wavtester_1.wav
    ON A NEW THREAD D:\UnderTest\wavtester_2.wav
    ON A NEW THREAD

What does this output indicate ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:32:28+00:00Added an answer on May 24, 2026 at 10:32 pm

    The two references refer to the same instance of BufferedReader.

    Therefore, if one thread reads something the next or the same thread continues to read from the last position.

    In your second example you create two different readers which have different internal states (read position).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a C# program that tries Marshal.SizeOf on a few different types: using
Here is a special Haskell program which outputs a Python program that outputs a
I've got a program that worked until recently. The offending code is shown here:
Here's a piece of code that takes most time in my program, according to
first question here. I'm developing a program in C# (.NET 3.5) that displays files
I am writing a PHP program which reads file with file_get_contents then attempts to
I am writing a java program that needs a file open dialog. The file
I'm using a Java program to POST some XML data from a remote client
I have a program that creates an object for each file in a directory
Im trying to write a program that limits the password to three incorrect attempts

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.