Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet and thread synchronization

 
daniele ippoliti
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salve ragazzi,

I have a servlet that receives requests and it serves them with service(...) method and for every request it is created a new thread to take care about new request. (is it right?)

So if i have a method that modifies static variables on a class and these variables are then reused to estabilish for other request if I can process the request or wait...Should I need some synchronize structures (block synchronize....)


I post here the code


for every request I invoke the method creating a new instance of the class new A().process(...)



double request served in the same moment

if I have theread 1 that stops his computations before instruction
equestToSchedule=false; (quella dopo il while) and then I have another request served by a thread 2 that it's stopped to the same instruction of thread 1, with this scenario I have passed all the checks (if and while) and with Thread 1 and Thread 2 I'll do 2 request at the same time and I would like to avoid it but to serve one request individually and then processing the other.

Moreover while i'm processing 1 request I receive other 100 requests It's enough to lunch only one time the request because all will be served with a single request.


My solution is to introduce a static object to synchronize threads:






I don't know if this could be a solution and overall if it is the more efficient....

thank you
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Synchronizing over an object should serve the purpose. Instead of introducing a new object you can utilize the "requestToSchedule" object (User wrapper Boolean instead of boolean).

Waiting for comments from others.

 
daniele ippoliti
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I think that we have a problem if i synchronize all the method because if I'm processing a request and it comes other 100 reuqest I want lunch the method only one time and not 100 time as now...

then I think that if i don't allow to other request to enter in my code end mybe if I have a request (processing = true ) they can't go in the else brench and set that there is a new request...in this way the while structure it losts of meaning...
so I realize that i have to write the code in a different way I think....but I'm thinking how....
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
don't synchronize the methods, but synchronized block
 
daniele ippoliti
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could you post your solution? because i have the while that read the variable so i don't know how to have a block and not synchronize all the methdos

thank you

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic