1 Reply Latest reply on Jan 7, 2008 2:29 PM by 807603

    [multithread] newbie here ... is inputStream shared?

      Hi, a newbie here. I've been playing with java for six month now and it's been pretty fun. Looking forward to learn more about java from this forum. Now I have a question about this little project I've been doing.

      I'm currently developing a multi-threaded client-server application (both client and server are multi-threaded). The idea is very easy which is to create threads that send http request to one server which will send back XML to the
      corresponding thread.

      I've managed to make it work. My application can now make http request and get the XML it's looking for. However, problem occurs when I try this scenario:

      1. Client Thread 1 http request> server -> creates server thread 1

      I made Server Thread 1 sleep for awhile so Client Thread 1 has to wait for the response.

      Then, while Server Thread 1 is still on sleep, I make another http request:

      2. Client Thread 2 http request> server -> creates server thread 2

      I again Server Thread 2 sleep for awhile so Client Thread 2 has to wait for the response.

      Now, Server Thread 1 wakes up and returns the XML response message. Now the funny thing is the Client Thread that accepts this response is not Client Thread 1, but Client Thread 2:

      3. Server Thread 1 wakes up -- XML response --> Client Thread 2 reads the response instead of Client Thread 1.

      What could be the problem!? What's also weird to me is that, if I try to do this scenario using a browser (by executing http request), the problem doesn't happen. Client Thread 1 returns to Server Thread 1 and Client Thread 2 returns to Server Thread 2.

      The code I wrote consist of a thread objects which has it's own private UrlConnection, InputStream, OutputStream, etc.

      This is a snippet of my code:

      public class Process {

      // this is how I create my threads
      public void execute(DataSource datasource, ADData ad)
      HttpProcess httpRequestor;

      httpRequestor = new HttpProcess(datasource,ad);
      catch (Exception e)

      public class HttpProcess extends Thread {

      private URL url;
      private HttpURLConnection urlConn;
      private DataSource ds;
      private ADData adData;
      private BufferedReader d;
      private BufferedWriter t;
      private InputStream in;
      private OutputStream out;
      private DataInputStream din;
      private DataOutputStream dout;

      public HttpProcess (DataSource datasource, ADData ad)
      this.ds = datasource;
      this.adData = ad;

      // this is how I implement my run method
      public void run ()
      String urlmsg = "http://localhost:9050" +
      "/?field1=" + adData.getField1()+
      "&field2=" + adData.getField2()+
      "&field3="+ adData.getField3();

      System.out.print("Sending URL message : " + urlmsg);

      url = new URL(urlmsg);
      urlConn = (HttpURLConnection)url.openConnection();
      urlConn.setDoOutput (true);

      out = new BufferedOutputStream(((HttpURLConnection)urlConn).getOutputStream());
      dout = new DataOutputStream(out);
      t = new BufferedWriter(new OutputStreamWriter(dout));

      in = new BufferedInputStream(((HttpURLConnection)urlConn).getInputStream());
      din = new DataInputStream(in);
      d = new BufferedReader(new
      String inputStr;

      if ((inputStr = d.readLine())!=null)
      XMLParser xmlparser = new

      listening = false;

      I thought I had it right by having private inputStream for each thread. but the behavior looks like the inputStream is shared by the two threads. However, I'm still new with java so I know my conclusion worth zero. Has anybody ever bumped into this kind of problem? Looking forward to hear back what you guys think. I'm just really really really curious about this problem

      Thanks so much.
        • 1. Re: [multithread] newbie here ... is inputStream shared?
          Please use code tags ([code] and [/code]). It's also quite possible the problem is in your server code, or your test jig, etc. You should post a small example that replicates your problem.

          Also, the while(listening) loop looks really screwy. I'm not sure what's going on there (why would you wrap your input stream inside a loop, especially since you close the input stream in the loop)?