At 04:31 PM 7/20/99 +0000, you wrote:
>Claude Zervas wrote:
>
>> At 06:02 PM 7/20/99 +0200, you wrote:
>> >
>> >Hi Christopher,
>> >On Tue, 20 Jul 1999, Christopher T. Beers wrote:
>> >CTB|   I appreciate everyone help in this matter yesterday.  The porblem
>> >CTB| was the scope of my doPost() method.  Outside of the method I declare
>> >CTB|
>> >CTB| private Hashtable table = new Hashtable();
>> >
>> >You should never do this, because the doPost(), doGet() or
>> >service()-Method could be called from several threads _in parallel_ which
>> >means that your parameters could get munged if two peope connect to your
>> >servlet. You've to make sure that your methods are reentrant.
>> >
>> >In order to get this save, initialize your Hashtable in the processing
>> >method:
>> >
>> >  public void doPost (...) {
>> >    Hashtable table = new Hashtable();
>> >    .. do stuff
>> >  }
>> >
>> >If you need these parameters in other methods in your servlet (which is
>> >what I assume you want do), you've to pass the table as parameter .. no
>> >way around it ..
>>
>> Yes there is, just put the usage in a synchronized block:
>>
>>         synchronized ( table ) {
>>                 ....
>>                 table.put( obj );
>>                 ...
>>         }
>>
>
>This will work only if you put *all* the code that deals with the table inside
>the "synchronized" declaration, including clearing the old values.  However,
>it is going to have horrible effects on response time, because it means only
>one user can be executing the block at a time.
>
>If there's only one user, this doesn't make a lot of difference (although even
>one user can initiate multiple requests to a servlet at the same time) -- but
>you need to design servlets to keep in mind that multiple calls will be active
>simultaneously, and minimize the amount of stuff that has to be synchronized.
>Using a local variable version of the Hashtable (in the case above) completely
>eliminates the need to synchronize anything.

Sorry, I was assuming that the hashtable in question was being used
across multiple requests. If just needs to be created once per request
then, by all means create it.

If the data needs to be persistant across multiple requests then
using a synchronized block to access it does not affect multithreading
that much unless the operations inside the block are very expensive...




--
--------------------------------------------------------------
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
READ THE FAQ!!!!     <http://java.apache.org/faq/>
Archives and Other:  <http://java.apache.org/main/mail.html/>
Problems?:           [EMAIL PROTECTED]

Reply via email to