Hi Pierre-Yves, Sorry for the delay in responding but I was in meeting most of the day. I have annotated your suggestions below. Thanks, Billy. -----Original Message----- From: Pierre-Yves Saumont [mailto:[EMAIL PROTECTED]] Sent: 20 September 2001 09:02 To: [EMAIL PROTECTED] Subject: Re: Inconsistent behaviour in Servlet output Hi, Again, we will have to guess! One important information is the order of magnitude of userDepartmentsNo, userAnalysersNo, userControlsNo and userTestsNo. As you are using nested loops, it could result in a quite long execution time and increase dramatically the impact of the synchronisation problem. If the second request is issued before the first one has completed, you are having synchronisation problem because you are using what you call Class level variables. > [The nested loops are all small (values around 1 or 2 during testing and only slightly more expected during go-live). Execution time is only around 15-20 seconds (currently) for completion of all cycles of the 4 loops and drawing of all graphs.] However, are making a little confusion. The nearest approximation to 'Class variables' should be static variables. If you do not declare your variables static, they can be members or local. As you say you are declaring your variables right after the class declaration, you are probably using members. This kind of variables are shared by all threads going through your servlet. >[I was only declaring some variable there because I though I might need easy access to them from all methods but this proved not to be the case so (as suggested by Milt) I made them local to the method containing the loops and the bug remains.] TotalPlotsDrawn doesn't cause problem in your code because it's only written to. TotalScreensDrawn, PlotsDrawn have to be taken care off because they are incremented and used for computation. To avoid the problem, you have to make this atomic, ie to synchonize the smallest possible block including write an read for the variables, where the read operation is supposed to depends on the written value. Otherwise, another thread could have modified the value between the write and read operations. As you are using nested loops, its not straightforward deciding which block to synchronize. In any case, you might have to rewrite your code to minimize the size of this block. Synchronizing the whole method works, but there is a drawback: The longer it takes to execute your method, the more penalty you get by synchronising the whole method, although the more you need to synchronize! So it's even more important to reduce the size (in terms of execution time, not line of code) of the synchronized block. >[The user base will be very small here (approx 10 users initially) and the service provided by the servlet probably only called by each user once or twice per day (viewing of Quality Control data & graphs), so hopefully there will not be too much of a problem if I cannot resolve the bug and have to run with Synchronisation] There are other techniques that can be used to avoid synchronisation. For example, you could use local copy of the variables and update the member variables in one place. That way, you might not need to use synchronisation at all or, if the the variables are to be updated atomically, yo might use the smallest possible synchronized block. However, you should pay attention to the two following points: 1) You should determine why the second request is sent and try to solve this problem in any case. >[I'm working on this (no luck so far) and still believe this is where the first bug lies] 2) If you solve this problem, you should still solve the synchronisation problem, at least if more than one client is to access your serlvet. (If two clients access the servlet simultaneously, you will face the same problem.) >[Agreed, that's why I say 'first' bug above - I realise the Synchronisation problem remains afterwards]. >[Many thanks for all the useful suggestions from yourself and others]. >Regards, Billy. Pierre-Yves -----Message d'origine----- De : A mailing list for discussion about Sun Microsystem's Java Servlet API Technology. [mailto:[EMAIL PROTECTED]]De la part de Graham, Billy Envoy� : mercredi 19 septembre 2001 17:31 � : [EMAIL PROTECTED] Objet : Re: Inconsistent behaviour in Servlet output
***************************************************************** This email has been Virus Scanned. Privileged/Confidential Information and/or Copyright Material may be contained in this e-mail. The information and Material is intended for the use of the intended addressee. If you are not the intended addressee, or the person responsible for delivering it to the intended addressee, you may not copy or deliver it to anyone else or use it in any unauthorised manner. To do so is prohibited and may be unlawful. If you receive this e-mail by mistake, advise the sender immediately by using the reply facility in your e-mail software. Thank you. Information Technology Department Belfast City Hospital Trust *****************************************************************
