58

Possible Duplicate:
What does threadsafe mean?

I am very confused that any class is Thread safe. I am understanding that, if any class is thread safe then it has some specific on its methods(as synchronized). Is it right or wrong? Please help me by elaborating on the meaning of it.

Community
  • 1
  • 1
Anuj
  • 601
  • 1
  • 6
  • 4

2 Answers2

156

As Seth stated thread safe means that a method or class instance can be used by multiple threads at the same time without any problems occuring.

Consider the following method:

private int myInt = 0;
public int AddOne()
{
    int tmp = myInt;
    tmp = tmp + 1;
    myInt = tmp;
    return tmp;
}

Now thread A and thread B both would like to execute AddOne(). but A starts first and reads the value of myInt (0) into tmp. Now for some reason the scheduler decides to halt thread A and defer execution to thread B. Thread B now also reads the value of myInt (still 0) into it's own variable tmp. Thread B finishes the entire method, so in the end myInt = 1. And 1 is returned. Now it's Thread A's turn again. Thread A continues. And adds 1 to tmp (tmp was 0 for thread A). And then saves this value in myInt. myInt is again 1.

So in this case the method AddOne() was called two times, but because the method was not implemented in a thread safe way the value of myInt is not 2, as expected, but 1 because the second thread read the variable myInt before the first thread finished updating it.

Creating thread safe methods is very hard in non trivial cases. And there are quite a few techniques. In Java you can mark a method as synchronized, this means that only one thread can execute that method at a given time. The other threads wait in line. This makes a method thread safe, but if there is a lot of work to be done in a method, then this wastes a lot of time. Another technique is to 'mark only a small part of a method as synchronized' by creating a lock or semaphore, and locking this small part (usually called the critical section). There are even some methods that are implemented as lockless thread safe, which means that they are built in such a way that multiple threads can race through them at the same time without ever causing problems, this can be the case when a method only executes one atomic call. Atomic calls are calls that can't be interrupted and can only be done by one thread at a time.

Shubham
  • 2,847
  • 4
  • 24
  • 37
Roy T.
  • 9,429
  • 2
  • 48
  • 70
  • 1
    thanx,but just one more thing. since StringBuffer is thread safe means it has all methods Synchronized implicitly ,is it? – Anuj Jun 12 '11 at 19:45
  • 3
    No, as I just told you synchronized is 'a way' to make things thread safe, but there are other ways. You can check in the StringBuffer source code what techniques they used. – Roy T. Jun 12 '11 at 19:47
  • 8
    For completeness, it is perhaps also worth saying explicitly that that methods which don't change state are thread safe without any extra effort. For example `public double PI () { return 3.14; }` – Frank Boyne Jun 12 '11 at 19:48
  • I agree with @Seth Carnegie; Thread safe doesn't related with synchronization of data access. It is only related with multiple threads can modify collection at the same time. – Kanagavelu Sugumar Oct 25 '13 at 07:32
  • If thread 'A' executes until to line 3 of the method, the results may be 2 – SFernando Aug 15 '15 at 05:59
  • @RoyT. Very helpful. Did you mean, it wastes a lot of "time" or "space" or both. – user104309 Apr 14 '16 at 11:04
  • @user104309 sorry I meant time. not space :) – Roy T. Apr 16 '16 at 11:02
  • Please follow Java convention, i.e. method names start with lowercase. – John Mikic Dec 20 '19 at 13:50
41

Thread safe simply means that it may be used from multiple threads at the same time without causing problems. This can mean that access to any resources are synchronized, or whatever.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • 3
    Most code can be called from multiple threads concurrently. Doesn't make it thread safe. – Oded Jun 12 '11 at 19:21
  • @Oded what makes it thread safe? – Seth Carnegie Jun 12 '11 at 19:22
  • That's the question. Just being _able_ to call a class from multiple threads means nothing. And "without causing problems" doesn't really say much either. – Oded Jun 12 '11 at 19:23
  • has class (that is thread safe) all instance methods are Synchronized implicitly – Anuj Jun 12 '11 at 19:28
  • 4
    @Oded what you say is true, but I think you are overlooking the "without causing problems" part of @Seth's answer. – Frank Boyne Jun 12 '11 at 19:31
  • 1
    @Frank - That bit wasn't there when I posted my comment. – Oded Jun 12 '11 at 19:32
  • 5
    +1. This answer is correct and is as specific as it can be given the context of the OP. Thread safety is exactly what he says: the ability to have multiple threads call a function simultaneously without problems. What constitutes "problems" and how to effect the protection are different questions altogether, but no single problem or single solution could be used interchangeably with "thread safety". – Adam Robinson Jun 12 '11 at 19:33
  • @Oded yet you refer to it in your comment... – Seth Carnegie Jun 12 '11 at 19:33
  • @Seth: FWIW, "can" would likely be the preferable form here ;) – Adam Robinson Jun 12 '11 at 19:34
  • Yes, Seth, in my _second_ comment. – Oded Jun 12 '11 at 19:34
  • 1
    @Anuj: No, synchronization is not the same thing as thread safety. Synchronization is a particular solution (access control) to a particular problem (or set of problems) within the realm of thread safety, but just because a method is "thread safe" does not mean that it is synchronized, nor does a method being synchronized mean that it is "thread safe". – Adam Robinson Jun 12 '11 at 19:35
  • @Adam yeah that's how it was but I changed it because Oded said "Just being _able_ to call a class from multiple threads means nothing". Since may means are allowed to and can means are capable of, I thought using may would satisfy oded. – Seth Carnegie Jun 12 '11 at 19:36
  • @Seth: My comment was more tongue-in-cheek, so take it for what it's worth; I don't see anyone being confused about the meaning of the statement purely by your choice of "can" or "may". – Adam Robinson Jun 12 '11 at 19:37
  • @Oded Sorry, I should look more carefully at the various timestamps. – Frank Boyne Jun 12 '11 at 20:00