Win a copy of Rust Web Development this week in the Other Languages 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Could you explain the behavior of a simple servlet?

 
Greenhorn
Posts: 17
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a newbie. I do first steps in servlets.



I use NetBeans IDE 7.3 & GlassFish Server 3.1.2.

I open my JSP several times in a few tabs (Firefox) and send a request to my servlet. Every time i have a result 50. Every next time when i wait a response i need to wait (i guess there is a delay because a servlet is sleeping. But i thought so... There is only one servlet class. Will i have only one instance of servlet for all requests or N-instances of servlet for N-requests? And if i have separated threads for each request (a new thread for a new request) - why i have delay with response? (every time i need to wait longer)?
In fact i expected to have an concurrent access problem (100 -50 -50 -50...)
Thanks.
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Will i have only one instance of servlet for all requests or N-instances of servlet for N-requests?


You will have one instance, so the "a" field will be shared between all threads, whether they happen concurrently or not.
 
Ranch Hand
Posts: 530
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have a delayed response because you wrote:

Thread.sleep(5000)

inside the servlet's processRequest() method.
 
Alex Pi
Greenhorn
Posts: 17
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:

Will i have only one instance of servlet for all requests or N-instances of servlet for N-requests?


You will have one instance, so the "a" field will be shared between all threads, whether they happen concurrently or not.



But why do i have always the result 50.
I thought i will subtract 50 every time. (the result will be 50, 0, -50, -100... so on)
 
Alex Pi
Greenhorn
Posts: 17
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nam Ha Minh wrote:You have a delayed response because you wrote:

Thread.sleep(5000)

inside the servlet's processRequest() method.



Yes. But it's not so easy.
I modified my code like that:


(4 requests from different tabs) produced following result:

Value is: 50
Thread name is: http-thread-pool-8080(5)

Value is: 50
Thread name is: http-thread-pool-8080(4)

Value is: 50
Thread name is: http-thread-pool-8080(1)

Value is: 50
Thread name is: http-thread-pool-8080(2)

It looks like i have different instances for each request-thread, and different threads for each request. Is it correct?
 
Greenhorn
Posts: 18
Netbeans IDE Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alex Pi wrote:

Ulf Dittmer wrote:

Will i have only one instance of servlet for all requests or N-instances of servlet for N-requests?


You will have one instance, so the "a" field will be shared between all threads, whether they happen concurrently or not.



But why do i have always the result 50.
I thought i will subtract 50 every time. (the result will be 50, 0, -50, -100... so on)




Hi ,

The code is fine and working correctly.

Actually you should try to go through your model class (Asynch) once again.
here x is an instance variable, so it is initialized only once when you create a object of Asynch class. So in get method basically you are subtracting 50 from x which is 100 in first iteration. So if condition gets true in first iteration and x is assigned a new value i.e. 50. from now on if condition will never gets true because 50 > 50 is false. So from now on each iteration the value of x remains unchanged and 50.

In this way you will get same value for each iteration.

 
Alex Pi
Greenhorn
Posts: 17
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh my Got... Thank you, my fault.... I wanted to create misleading behavior... i wanted to check simultaneous access...
 
Alex Pi
Greenhorn
Posts: 17
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed my code:


Hurra! I received what i wanted:
Value is: 60
Thread name is: http-thread-pool-8080(4)

Value is: 20
Thread name is: http-thread-pool-8080(5)

Value is: -20
Thread name is: http-thread-pool-8080(1)

I wanted to receive "Race condition"...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic