Results 1 to 2 of 2
- 02-24-2011, 10:10 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
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:
- I tried using but it doesn't have an effect.Java Code:
connection.setDoOutput(false);
- I tried closing the output stream () but it this lines throws an exception: "cannot write to a URLConnection if doOutput=false - call setDoOutput(true)"Java Code:
connection.getOutputStream().close();
- I tried writing to the output stream of the connection, but it stops (waiting for the readline to return) at the instead and runs normally at theJava Code:
new PrintStream(connection.getOutputStream()).print("BLAH!");Java Code:connection.connect();
- Note that the same behavior is experienced when using URL or URLConnection instead of HttpURLConnection (as basically they're using the same stream)
- I tried java.util.Scanner instead of BufferedStream but still the same behavior as expected.
- 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 helpLast edited by mab; 02-24-2011 at 10:16 AM.
- 02-24-2011, 12:21 PM #2
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
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.
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:Java Code:while (!cmd.equalsIgnoreCase("exit")) { System.out.print("test>"); while (!reader.ready()) {} cmd = reader.readLine(); System.out.println("echo>" + cmd); }
Similar Threads
-
URLConnection Efficiency
By Lil_Aziz1 in forum New To JavaReplies: 22Last Post: 08-19-2010, 06:27 PM -
URLConnection not timing out as expected
By StephenS in forum NetworkingReplies: 2Last Post: 04-02-2010, 03:10 AM -
3x wrong password will lock the system
By ashin in forum SWT / JFaceReplies: 0Last Post: 07-11-2009, 04:03 PM -
How to set request parameters to an URLConnection
By somesh A in forum NetworkingReplies: 0Last Post: 04-10-2009, 08:08 AM -
Opening URLConnection
By Java Tip in forum Java TipReplies: 0Last Post: 11-24-2007, 07:37 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks