This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Im facing problem of threads while calling different methods of different classes from Servelts. My scenario is that there could be different users those could access the same servlets simoltaneously & from the service method Im callind different methods of the beans/simple classes which are interactig with database also. If more then two users are accessing the same screen or other using the same servlet continuosly then its giving abrupt result. I know that every time the servlets called having the same instance but re exicuted the service methods. But I dnt know wat is happening. And finnaly I made all the methods of my bean as sysnchronized & variables as volatile & finally made the call of the classes from servlet from the Synchronized block itself. But still get errors. can any body plz help me. Its very urgent.Plz..... regds, Manish Malhotra
And instance variables are not thread safe. The only thing thread safe in a Servlet are local variables. See my last night reading helped already. If you are calling JavaBeans/other classes from your Servlet you need to make them threadsafe the same way as in any Java class would. Mark
Originally posted by Mark Spritzler: And instance variables are not thread safe. The only thing thread safe in a Servlet are local variables. See my last night reading helped already. If you are calling JavaBeans/other classes from your Servlet you need to make them threadsafe the same way as in any Java class would. Mark
Thx to both of u 4 ur reply. But can u tell me that how to make the JavaBeans/other classes objects threadsafe as u r telling. Can I make all the variables locally defined in the method srevice & then made the service method synchronized. Right now I hv done the service method & class methods synchronized & make bean variables as volatile.Its seems to be working fine. But , I think it is not a good practice becz it will overloaded the servlet/classes. Can anybody of u just give me more description. Redgs, Manish Malhotra
Well you shouldn't synchronize the service method. If you want the service method synchronized just implement the SingleThreadModel interface. Now to make the JavaBeans/other classes to be thread safe you need to use the Synchronized keyword on those methods in those classes. Mark