File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Two threads executed but expected one Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Two threads executed but expected one" Watch "Two threads executed but expected one" New topic
Author

Two threads executed but expected one

Martin Johansson
Greenhorn

Joined: Sep 17, 2012
Posts: 3
I'm quite new to threads and just wonder why this example executes 2 threads when I expect 1 thread to be executed



Output from from HttpServer class:
Thread-1
Thread-2

Expected output:
Thread-1

What am I doing wrong?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Hi Martin, welcome to the Ranch!

So, what are you doing wrong? You're expecting the wrong output. Your server code is telling you that two connections have been made to your server. For some reason you're under the impression that only one connection was made, but you haven't posted any code which shows how the connections are being made, so it's impossible to tell why you might incorrectly think that.
Martin Johansson
Greenhorn

Joined: Sep 17, 2012
Posts: 3
Paul Clapham wrote:Hi Martin, welcome to the Ranch!

So, what are you doing wrong? You're expecting the wrong output. Your server code is telling you that two connections have been made to your server. For some reason you're under the impression that only one connection was made, but you haven't posted any code which shows how the connections are being made, so it's impossible to tell why you might incorrectly think that.


Thank you.

When the code above is executed and I type http://localhost:9898 in the browser, a connection is established but I thought just one thread would execute, not two. Maybe I write the code in the wrong way?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

You're assuming that your browser only sends one request to the URL. Confirm that assumption.
Martin Johansson
Greenhorn

Joined: Sep 17, 2012
Posts: 3
Paul Clapham wrote:You're assuming that your browser only sends one request to the URL. Confirm that assumption.


When I "inspect element" and check the "network tab" in chrome it says that 1 request is done, but still 2 threads are created to handle the request in my code.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rather than guessing or relying on what Chrome tells you, get the answer straight from the source. Print out the details of each request that you process--which IP and port it came from, whether it's a GET, POST, etc., any parameters, and so on.

 
jQuery in Action, 2nd edition
 
subject: Two threads executed but expected one