Results 1 to 2 of 2
  1. #1
    mab
    mab is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Question Lock between URLConnection and System.in

    I've created a program that has two threads; one for taking user input and the other for doing HttpURLConnection communication. A sample code is attached below.

    The problem is: HttpURLConnection.connect() waits for reader.readLine() that reads from the standard input to return, before connect returns.

    Some diagnosis:
    1. I tried using
      Java Code:
      connection.setDoOutput(false);
      but it doesn't have an effect.
    2. I tried closing the output stream (
      Java Code:
      connection.getOutputStream().close();
      ) but it this lines throws an exception: "cannot write to a URLConnection if doOutput=false - call setDoOutput(true)"
    3. I tried writing to the output stream of the connection, but it stops (waiting for the readline to return) at the
      Java Code:
      new PrintStream(connection.getOutputStream()).print("BLAH!");
      instead and runs normally at the
      Java Code:
      connection.connect();
    4. Note that the same behavior is experienced when using URL or URLConnection instead of HttpURLConnection (as basically they're using the same stream)
    5. I tried java.util.Scanner instead of BufferedStream but still the same behavior as expected.
    6. One thing to note is in the original project there are other connections made and succeeds with no problem, it's always the first one that halts.


    Here's sample code:
    Java Code:
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class Main {
    
        public static void main(String[] args) {
            try {
                Nono n = new Nono();
                Thread t = new Thread(n, "newThread");
                t.start();
                BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
                String cmd = "";
                while (!cmd.equalsIgnoreCase("exit")) {
                    System.out.print("test>");
                    cmd = reader.readLine();
                }
                n.state = 1;
            } catch (Exception e) {
                System.err.println(e.getMessage());
            }
        }
    
        public static class Nono implements Runnable {
    
            public int state = 1;
    
            public void run() {
                try {
                    while (state == 1) {
                        Thread.sleep(100);   // used to avoid overloading server
                        HttpURLConnection connection = (HttpURLConnection) new URL("http://www.google.com").openConnection();
                        //connection.setDoOutput(false);
                        //connection.getOutputStream().close();
                        //connection.setDoOutput(true);
                        //new PrintStream(connection.getOutputStream()).print("BLAH!");
                        connection.connect();
                        System.out.println("THREAD> connection established");
                        connection.disconnect();
                    }
                } catch (Exception e) {
                    System.err.println(e.getMessage());
                }
            }
        }
    }

    Thanks for your help
    Last edited by mab; 02-24-2011 at 11:16 AM.

  2. #2
    mab
    mab is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    It seems that all IO for a given process (including all its threads) becomes blocked for one single IO request. So the problem can be solved by calling the blocking read only when there is available data.
    Java Code:
    while (!cmd.equalsIgnoreCase("exit")) {
                    System.out.print("test>");
                    while (!reader.ready()) {}
                    cmd = reader.readLine();
                    System.out.println("echo>" + cmd);
                }
    But still I was wondering if there is a smarter solution than using the availability while loop + why this problem appears only for the first URLConncetion object but not for subsequently created ones?! :confused:

Similar Threads

  1. URLConnection Efficiency
    By Lil_Aziz1 in forum New To Java
    Replies: 22
    Last Post: 08-19-2010, 07:27 PM
  2. URLConnection not timing out as expected
    By StephenS in forum Networking
    Replies: 2
    Last Post: 04-02-2010, 04:10 AM
  3. 3x wrong password will lock the system
    By ashin in forum SWT / JFace
    Replies: 0
    Last Post: 07-11-2009, 05:03 PM
  4. How to set request parameters to an URLConnection
    By somesh A in forum Networking
    Replies: 0
    Last Post: 04-10-2009, 09:08 AM
  5. Opening URLConnection
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-24-2007, 08:37 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •