File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Could you explain the behavior of a simple servlet?

 
Alex Pi
Greenhorn
Posts: 17
Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Nam Ha Minh
Ranch Hand
Posts: 502
Eclipse IDE Firefox Browser Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Bhavesh Saraswat
Greenhorn
Posts: 18
Java Netbeans IDE Oracle
  • 1
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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"...
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic