Synchronization does produce overhead, but it's what you *must* do if
you will not re-write your servlets to no longer contain instance
fields, *and* you wish to provide thread safety.
 
There is a mode, and you have mentioned it before.. It's not a Tomcat
mode, it's that "Single threaded" thing. (see, I can't even write it
correctly, I don't even know what the exact name is).

However, the decision to go with 'single threaded' will mostly be met
with howls of derision and scorn... many people (correctly) believe this
to be a poor idea.  However, since you're already done a lot of work,
and you're not willing to re-write.. it's the option that you're left
with.
 
Another topic you seem worried about is that one user requesting his 300
emails will cause other people (perhaps with only 10 or 20 emails) to
wait.  I'm perhaps completely off my rocker, but I think this is handled
by having servlet multi-threaded in the first place.
 
If the request that wants the 300 emails is taking "too long" (as
decided by the container), then that user's request thread is suspended,
while the other user's requests are handled.  That's the whole point of
multi-threading the servlet... (so that requests don't pile up)?
 
Yoav.. is that completely wrong?  

> -----Original Message-----
> From: kwirirai [mailto:[EMAIL PROTECTED] 
> Sent: Monday, February 09, 2004 1:12 PM
> To: Tomcat Users List
> Subject: Re: Servlet thread safety in Tomcat
> 
> 
> But if  I am to synchronise my code blocks won't that slow done the 
> application, or is there a "mode" that you can put Tomcat 
> into so that 
> it will create a new instance
> servlet thread  with its own variables and execution space and stuff 
> like that .Because I was thinking  putting all those 
> synchronized blocks 
> will slow down the app.
> 
> David Ramsey wrote:
> 
> >Your primary mistake seems to be in assuming that Tomcat 
> will create a 
> >new instance of a servlet for every thread started. This is not what 
> >happens, therefore servlet instance variables are not thread safe 
> >unless you take additional actions to make them safe. Likewise, 
> >application global entities, such as singletons, won't be 
> thread safe 
> >without taking additional actions on your part. The short rule is 
> >simply if two threads might access this data concurrently, then you 
> >need to protect yourself from such a collision. About the only thing 
> >that is mostly thread safe are objects whose scope is limited to a 
> >single method, but even these can get you in trouble if you put 
> >references to those objects in global locations (such as an 
> application 
> >wide cache).
> >
> >
> >--- kwirirai <[EMAIL PROTECTED]> wrote:
> >  
> >
> >>The app is simply meant to grab some mails from a pop server and 
> >>simply display it using Java Mail.
> >>I realise part of my mistake is using global variables
> >>,unsynchronized 
> >>collections and unsynchronized code blocks.I am not using 
> the single 
> >>thread model.
> >>My initial thought ( :-)  forgive me its some time since I 
> have coded
> >>
> >>servlets) was that Tomcat will create a new servlet instance that is
> >>totally independed of the other,  for each request.On 
> testing the app
> >>on 
> >>two client machines I have realized that the data is corrupted all
> >>mixed 
> >>up and my velocity template is throwing an error  caused by
> >>concurrent 
> >>modification.
> >>My question is there some way of making this app thread 
> safe and also
> >>
> >>how does Tomcat  actually many requests , in terms of the  threading
> >>method used?I am using Tomcat 4.1
> >>
> >>
> >>Shapira, Yoav wrote:
> >>
> >>    
> >>
> >>>Howdy,
> >>>State your specific requirements and we can help you design 
> >>>servlets/objects that will meet those requirements.  Your original
> >>>      
> >>>
> >>post
> >>    
> >>
> >>>is too broad to solicit a detailed response.
> >>>
> >>>Yoav Shapira
> >>>Millennium ChemInformatics
> >>>
> >>>
> >>> 
> >>>
> >>>      
> >>>
> >>>>-----Original Message-----
> >>>>From: kwirirai [mailto:[EMAIL PROTECTED]
> >>>>Sent: Monday, February 09, 2004 12:48 PM
> >>>>To: [EMAIL PROTECTED]
> >>>>Subject: Servlet thread safety in Tomcat
> >>>>
> >>>>Hi All
> >>>>I am developing an application that uses JavaMail.What I am
> >>>>        
> >>>>
> >>concered is
> >>    
> >>
> >>>>the issue of thread safety,and efficiency.My question is do I need
> >>>>        
> >>>>
> >>to
> >>    
> >>
> >>>>employ synchronized blocks in my Servlet code or is there another
> >>>>        
> >>>>
> >>way
> >>    
> >>
> >>>>   
> >>>>
> >>>>        
> >>>>
> >>>to
> >>> 
> >>>
> >>>      
> >>>
> >>>>implement thread safety.I have been experimenting with the
> >>>>        
> >>>>
> >>application
> >>    
> >>
> >>>>and I have seen that the data is actualy mixing up.I realy need to
> >>>>        
> >>>>
> >>now
> >>    
> >>
> >>>>how Tomcat handles request and issues those request ,this is in 
> >>>>connection with threading. I have thought about the single thread
> >>>>        
> >>>>
> >>model
> >>    
> >>
> >>>>in my servlets but I think this is an inefficient method to use.
> >>>>
> >>>>Thanks
> >>>>Kwiri
> >>>>
> >>>>
> >>>>
> >>>>
> >>>>        
> >>>>
> >>>-----------------------------------------------------------
> ----------
> >>>      
> >>>
> >>>>To unsubscribe, e-mail: [EMAIL PROTECTED]
> >>>>For additional commands, e-mail:
> >>>>        
> >>>>
> >>[EMAIL PROTECTED]
> >>    
> >>
> >>>>   
> >>>>
> >>>>        
> >>>>
> >>>
> >>>
> >>>This e-mail, including any attachments, is a confidential business
> >>>      
> >>>
> >>communication, and may contain information that is confidential, 
> >>proprietary and/or privileged.  This e-mail is intended only for the
> >>individual(s) to whom it is addressed, and may not be 
> saved, copied, 
> >>printed, disclosed or used by anyone else.  If you are not the(an) 
> >>intended recipient, please immediately delete this e-mail from your 
> >>computer system and notify the sender.  Thank you.
> >>    
> >>
> >>>      
> >>>
> >>------------------------------------------------------------
> ---------
> >>    
> >>
> >>>To unsubscribe, e-mail: [EMAIL PROTECTED]
> >>>For additional commands, e-mail: 
> [EMAIL PROTECTED]
> >>>
> >>>
> >>> 
> >>>
> >>>      
> >>>
> >>    
> >>
> >
> >
> >__________________________________
> >Do you Yahoo!?
> >Yahoo! Finance: Get your refund fast by filing online. 
> >http://taxes.yahoo.com/filing.html
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: [EMAIL PROTECTED]
> >For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
> >  
> >
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to