What is thread safe in java
Labels: best practices , core java , Java multithreading Tutorials , thread interview questions. January 11, at AM vasanth said Super April 30, at AM Unknown said December 9, at PM Anonymous said May 27, at AM Tanmay said April 9, at PM Anonymous said June 15, at PM Anonymous said February 3, at AM javin paul said February 6, at AM Anonymous said March 11, at AM Unknown said NIce comments April 28, at AM Blog do armando said March 4, at AM.
Newer Post Older Post Home. Subscribe to: Post Comments Atom. Subscribe for Discounts and Updates Follow. Search This Blog. Secure Hash Algorithms. Best Way to Learn Java. How to Start New Blog. Skip to content. Java Concurrency — Thread Safety? Last Updated: August 27, Java Concurrency. A piece of code is thread-safe if it only manipulates shared data structures in a manner that guarantees safe execution by multiple threads at the same time.
And there are more similar definitions. What is Correctness in thread safety? Correctness means that a class conforms to its specification. Example: A Stateless Servlet A good example of thread safe class is java servlets which have no fields and references, no fields from other classes etc. Happy Learning!! Quiescent means that we need to make sure that no other methods are currently running when we call the quiescent method. The following example shows how to use the quiescent method size of the ConcurrentHashMap :.
By waiting until all threads are finished using thread join, we make sure that no other threads are accessing the ConcurrentHashMap when we call the method size. The method size uses a mechanism also used in the class java. Instead of using a single variable for storing the current size, it uses an array. Different threads update different parts of the array, thereby avoiding contention. The algorithm is explained in more detail in the Java doc of Striped The quiescent classes and methods are useful for collecting statistics under high contention.
After you collected the data, you can use a single thread to evaluate the collected statistics. In theoretical computer science, thread safety means that a data structure fulfills a correctness criterion. The most common used correctness criterion is linearizable, which means that the methods of the data structure are atomic.
For common data structures exists a provable linearizable concurrent data structures, see the book The Art of multiprocessor programming by Maurice Herlihy and Nir Shavit. But to make a data structure linearizable, an expensive synchronization mechanism like compare and swap is needed, see the paper Laws of Order: Expensive Synchronization in Concurrent Algorithms Cannot be Eliminated to learn more.
Therefore, other correctness criteria like quiescent are investigated. So, I think the question is not "why are there no other types of thread-safe methods in Java? Currently, quiescent methods are only used to collect statistics, like the size of the ConcurrentHashMap. For all other use cases, atomic methods are used. Let us wait and see if the future brings additional types of thread-safe methods. Java Concurrency In-Depth. Improve this answer. Shubham 2, 3 3 gold badges 20 20 silver badges 35 35 bronze badges.
Roy T. 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. For completeness, it is perhaps also worth saying explicitly that that methods which don't change state are thread safe without any extra effort. 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. If thread 'A' executes until to line 3 of the method, the results may be 2 — SFernando. Show 3 more comments. Seth Carnegie Seth Carnegie 71k 20 20 gold badges silver badges bronze badges. Most code can be called from multiple threads concurrently. Doesn't make it thread safe. Oded what you say is true, but I think you are overlooking the "without causing problems" part of Seth's answer.
Frank - That bit wasn't there when I posted my comment. 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".
0コメント