Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: InputStreamReader blocking threads

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default InputStreamReader blocking threads

    Hey,

    I'm working on a multiuser server. For every client I create a thread. I however face issues with my InputStreamReader blocking the thread whenever multiple requests are made at the same time (in fact if the InputStream reader is blocking, every request is scheduled instead and executed once finished).

    I have a minimal example which demonstrated the problem:

    ServerTest.java:
        package servertest;
     
        import java.io.IOException;
        import java.net.ServerSocket;
        import java.net.Socket;
     
        public class ServerTest {
     
            public static void main(String[] args) throws IOException {
                ServerSocket server = new ServerSocket(1234);
                Socket s;
                while (true) {
                    s = server.accept();
                    ServerThread t = new ServerThread(s);
                    t.start();
       }}}


    ServerThread.java:
        package servertest;
     
        import java.io.BufferedReader;
        import java.io.BufferedWriter;
        import java.io.IOException;
        import java.io.InputStreamReader;
        import java.io.OutputStreamWriter;
        import java.net.Socket;
        import java.util.logging.Level;
        import java.util.logging.Logger;
     
        public class ServerThread extends Thread {
     
            private Socket s;
            public ServerThread(Socket s) {
                this.s = s;
            }
     
            @Override
            public void run() {
     
                try {
                    BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
                    String text = in.readLine();
                    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
     
                    System.out.println("Sleeping...");
                    Thread.sleep(8000);
                    out.write(text.toUpperCase());
                    out.newLine();
                    out.flush();
                    out.close();
                    in.close();
                } catch (IOException | InterruptedException ex) {
                   System.out.println("Error: " + ex);
                }
            }
        }


    Now whenever you request localhost:1234 twice, you will face one thread executing (Thread.sleep(8000)) while the second "waits" until the InputStreamReader is finished.

    I think this is a design problem. Will I need an additional thread just for the InputStreamReader? If so, where?:/

    This error has caused alot of headache to me already (first to find out that this was the actual cause, second the attempt to find a solution for this), so any help is highly appreaciated!


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: InputStreamReader blocking threads

    Quote Originally Posted by coffeecups View Post
    Will I need an additional thread just for the InputStreamReader?
    No. In your code there's nothing obviously wrong. The exception handling can be better, ok .... but apart this, what I see is technically correct.
    Now one question .... what is the code for the client?
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. Replies: 1
    Last Post: November 2nd, 2012, 02:21 PM
  2. Blocking format (important)
    By benglish in forum Java Theory & Questions
    Replies: 3
    Last Post: April 11th, 2011, 06:34 PM
  3. Question about blocking queue
    By Kerr in forum Threads
    Replies: 2
    Last Post: April 8th, 2011, 04:59 AM
  4. blocking a network connection
    By BraveProgrammer in forum Java Networking
    Replies: 4
    Last Post: September 2nd, 2010, 12:08 PM
  5. How to Read user input from the console with InputStreamReader in Java?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:48 AM