This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Thread safety means that the object that is accessed by multiple threads is always in the valid and the consistent state. More precicely, it breaks down to safety in terms of 3 different concepts: deadlock, starvation and race. The deadlock most commonly occurs when one process requests a resource held by another process that is waiting for the my resource held by the first process. For example, if Hamas says "We will continue the terror until you leave the territory", and Israel says "We will not leave the territory until you stop terror", that's a classic deadlock. Starvation is when a process is waiting indefinitely for a resource to become free. For example, you could design the traffic light so that it switches the light every 5 hours, and it will do the job in the sense of preventing the collisions, but it's not really a good solution, is it? Race conditions occur when two or more threads do not coordinate well and the result is undeterministic. For example, if two people talk at the same time, and the third person listens, he may hear a sentence tht doesn't make any sense. Moreover, depending on how fast the two people speak, the outcome will be different.
Joined: Sep 12, 2003
Thanks for the reply. But I would love if you can give me an example using threads so that I can get the correct picture. Please if any once can. Thanks once again.
Hi Chandra I tried to write an example but I was unsuccessful as I wanted to write a simple example....but you can refer to this where you can see what hazards thread safety ignorance can cause... Regards Maulin
One example we bump into is servlets. The server might have just one object instance of a servlet class. Every time a request comes in, the server starts a new thread and calls doGet() on the servlet. Thus, the servlet can be running in multiple threads for multiple users all at once. We have to be sure we write the servlet to be thread safe. One thing we must not do is use member variables. If we did something as simple as
we could be surprised to find another thread changes mCounter in between those two lines, what was called a race condition above. So most thread safe classes don't use member variables unless they want to share the value among all threads, and then they have to synchronize very carefully.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com