Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is thread Safe in java? [duplicate]

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.

like image 206
Anuj Avatar asked Jun 12 '11 19:06

Anuj


People also ask

What is a thread-safe in Java?

A thread-safe class is a class that guarantees the internal state of the class as well as returned values from methods, are correct while invoked concurrently from multiple threads. The collection classes that are thread-safe in Java are Stack, Vector, Properties, Hashtable, etc.

What is called thread-safe?

Thread safety is a computer programming concept applicable to multi-threaded code. Thread-safe code only manipulates shared data structures in a manner that ensures that all threads behave properly and fulfill their design specifications without unintended interaction.

Is thread-safe means synchronized?

To make it thread safe then, you have to force person 1 to wait for person 2 to complete their task before allowing person 1 to edit the document. Synchronized means that in a multiple threaded environment, a Synchronized object does not let two threads access a method/block of code at the same time.

How immutable objects are thread-safe?

To put it simply, a class instance is immutable when its internal state can't be modified after it has been constructed. A MessageService object is effectively immutable since its state can't change after its construction. So, it's thread-safe.


2 Answers

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.

like image 131
Roy T. Avatar answered Oct 06 '22 01:10

Roy T.


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.

like image 44
Seth Carnegie Avatar answered Oct 06 '22 00:10

Seth Carnegie