Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Two threads executed but expected one

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 27211
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 27211
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic