• Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlets and wait notify code  RSS feed

 
Siddharth Mehrotra
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ahve a normal servlet,
so as per the client requests it is going to spawn new threads for service method.
and for some condition i want to have a wait and notify methods in the servlet.
Is that okay.
cos someone on my office was sayingt that it is not advisable to use wait and notify in service methods of servlets.
but as far as my knowledge is concerened, i feel that if i want to keep data consistent across multiple thread we have to use waut and notify.
can anyone throw some light on this
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The servlet container is responsible for assigning a Thread to each request. Generally there is no reason for you to create your own Thread unless there is some job to be performed independently from handling a request.
The way you keep multiple requests (Threads) from interfering with each other is by NOT using any instance or static variables for data that changes between requests. Only use variables that are local to methods.
For variables that must be shared between requests, use the synchronized keyword to ensure that only one request (Thread) can use the variable at a time.
Any servlets book will discuss the problem in detail.
Bill
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!