Is this class thread safe?

consider this class, without instance variables and only non-synchronous methods, can we conclude from this information that this class is thread safe?

public class test{ public void test1{ // do something } public void test2{ // do something } public void test3{ // do something } } 
+4
source share
8 answers

An insecure thread means that if multiple threads try to access an object at the same time, something can change from one access to another and cause problems. Consider the following:

 int incrementCount() { this.count++; // ... Do some other stuff return this.count; } 

will not be thread safe. Why not? Imagine that thread 1 accesses it, count increases, then some processing happens. Having passed through a function, another thread calls it, increasing count again. The first stream from which he could go, say, from 1 to 2, will now have him from 1 to 3 when he returns. Topic 2 will see that it goes from 1 to 3, so what happened to 2?

In this case, you would like something like this (bearing in mind that this is not some language-specific code, but closest to Java, one of the 2 I worked with)

 int incrementCount() synchronized { this.count++; // ... Do some other stuff return this.count; } 

The synchronized should ensure that as long as one thread accesses it, no other threads can. This would mean that thread 1 gets into it, count goes from 1 to 2, as expected. Thread 2 gets into it, while 1 is processing, it should wait for thread 1 to complete. When this is done, thread 1 will receive a return of 2, then thread 2 will go to the checkmark and get the expected value of 3.

Now, an example similar to what you have, it will be completely thread safe, no matter what:

 int incrementCount(int count) { count++; // ... Do some other stuff return this.count; } 

Since the only variables affected here are completely local to this function, there is no case when two threads accessing it at the same time can try to work with data modified from another. This will make the thread safe.

So, to answer the question, assuming that the functions do not change anything outside the specific function being called, then yes, the class can be considered thread safe.

+3
source

It all depends on the state in which these methods mutate. If they do not change the general condition, they are thread safe. If they mutate only the local state, they are thread safe. If they only call methods that are thread safe, they are thread safe.

+6
source

Consider the following quote from an article on thread safety (“Java Theory and Practice: Defining Thread Safety”):

In fact, any definition of thread safety will have a certain degree of roundness, since it should attract the class specification - this is an informal, prosaic description of what the class does, its side effects, which states: are valid or invalid, invariants, premises, postconditions, etc. d. (The restrictions regarding the state of an object imposed by the specification apply only to the external visible state - what can be observed by calling its public methods and gaining access to its public fields, and not its internal state, which is actually private fields. )

Thread safety

For a class to be thread safe, it must first behave correctly in a single-threaded environment. If the class is correctly implemented, which is another way of saying that it meets its specifications, no sequence of operations (reading or writing public fields and calls to public methods) on objects of this class should be able to put the object in an invalid state, observe that the object is in invalid state, or violate any of the class invariants, preconditions or postconditions.

In addition, for a class that must be thread safe, it must continue to behave correctly, in the sense described above, when accessing from multiple threads regardless of the scheduling or rotation of these threads by the runtime, without additional synchronization by the calling code. The effect is that operations on a thread-safe object will be displayed for all threads in a fixed global consistent order.

Thus, your class is thread safe if it has no side effects. As soon as methods mutate any external objects (for example, some singletones, as mentioned by others), they are no more thread safe.

+2
source

Depends on what happens inside these methods. If they manipulate / call any method parameters or global variables / singleton that are not thread safe in themselves, the class is also not thread safe.

(yes, I see that the methods shown here have no parameters, but without brackets, so this is clearly not a complete working code - it won’t even compile as it is.)

+1
source

yes if there are no instance variables. method calls using only input parameters, and local variables are inherently thread safe. You might consider making static methods too to reflect this.

0
source

If it does not have a mutable state, it is thread safe. If you do not have a state, you are thread safe by association.

0
source

No, I do not think so.

For example, one of the methods could get a (insecure) singleton object from another class and mutate this object.

0
source

Yes - this class is thread safe, but that does not mean your application.

An application is thread safe if the threads in it cannot simultaneously access the heap state. All objects in Java (and therefore all their fields) are created on the heap. So, if there are no fields in the object, then this is thread safe.

In any practical application, objects will have a state. If you can guarantee that these objects will not be available at the same time, you have a thread protected application.

There are ways to optimize access to general state, for example. Atomic variables or using the volatile mobile dictionary, but I think this goes beyond what you requested.

Hope this helps.

0
source

Source: https://habr.com/ru/post/1307146/


All Articles