• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two threads executed but expected one

 
Martin Johansson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20719
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20719
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're assuming that your browser only sends one request to the URL. Confirm that assumption.
 
Martin Johansson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic