aspose file tools*
The moose likes Beginning Java and the fly likes What does thread safe means? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What does thread safe means?" Watch "What does thread safe means?" New topic
Author

What does thread safe means?

Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
Can anyone please explain what threadsafe means and i suppose this surely belongs to this forum
Thank YOu


Thanks,
Shekar
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
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.
Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
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.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
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
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: What does thread safe means?