|
-
Feb 4th, 2004, 01:11 PM
#1
Thread Starter
Hyperactive Member
Thread Safety for Servlets
I'm writing a servlet and I need to ensure it is thread safe.
How can I do that?
Thanks!
-
Feb 5th, 2004, 06:09 AM
#2
No generic way. Think about resources that one thread might access while another is modifying them. Make no assumptions about the order in which things happen. Then insert synchronization where necessary.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 6th, 2004, 01:44 AM
#3
Dazed Member
Yes CornedBee is right. There is really no generic way to write multithreaded code that trys to tap single resource. What you want to be careful of though is miss-synchronization or deadlock.
But, synchronization is not always the best possible soultion to the problem of inconsistent behavior as a result of thread scheduling. One technique used ensure thread safety is to use local variables instead of fields wherever possible since they do not have synchronization problems. Every time a method is entered the JVM creates a completely new set of local variables. The variables are destroyed when the method exits. This means that there is no possible way for a local variable to be used in teo different threads. Every thread has its own set of local variables.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|