Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concurrently invoking Java method of singleton object

I've got a question regarding multiple-thread method invocation in Java. Let's say we have a singleton object, and its class declared as follows:

public class SomeClass {
    public void someMethod(SomeValueObject object) {

        if (object.condition1) {
            ...
        }
        if (object.condition2) {
            ...
        }
        if (object.condition3) {
            ...
        }

    }
}

I'm wondering if this singleton object is being concurrently accessed and its someMethod called with distinct SomeValueObject instances, is there a chance some random thread change the reference of object for another thread's method invocation and mess up things? And what about fields created inside method scope? What I'm not aware of, is there any separate Method context being created for every thread invoking the method, or Method context is the same for all threads invoking it? If it is the latter case, I guess I need the synchronized keyword for thread safety, or use distinct SomeClass instances for every thread (in case I need faster execution over memory optimization). Would you please explain the matter for me?

P.S. Thanks for all of your answers guys!

like image 331
Martin Asenov Avatar asked Mar 03 '12 17:03

Martin Asenov


People also ask

What happens when two threads access singleton at same time?

Is singleton thread safe? A singleton class itself is not thread safe. Multiple threads can access the singleton same time and create multiple objects, violating the singleton concept. The singleton may also return a reference to a partially initialized object.

Can a singleton class accessed by multiple threads?

It can be used in a single threaded environment because multiple threads can break singleton property as they can access get instance method simultaneously and create multiple objects.

How do you handle singleton in multithreading?

Thread Safe Singleton in JavaCreate the private constructor to avoid any new object creation with new operator. Declare a private static instance of the same class. Provide a public static method that will return the singleton class instance variable.

Can we modify singleton object Java?

From your singleton class, if the object is created then no objects are allowed to modify the someNum & someString values because of a singleton pattern.


3 Answers

If everything is local, your method is thread-safe as is. Each thread will have its own object argument on the stack, and they won't interfere with each other.

You could have concurrency problems if two threads invoke this method with the same object as argument, or if two of those objects share some state, but that's not the problem of the singleton. It's the problem of the shared state, which must be properly synchronized.

Good rule of thumb: a stateless object is thread-safe. An object with immutable state is thread-safe. An object with mutable state is not thread-safe if it doesn't properly synchronize access to the shared state.

like image 104
JB Nizet Avatar answered Oct 26 '22 13:10

JB Nizet


No, threads won't be able to change the local variables of a different thread.

All variables created in the method's scope [local variables] - including parameters are allocated on the specific thread's stack, and thus are not shared between two threads.

However - all class's fields are unsafe, and if one thread changes them - it will be reflected in all.

like image 36
amit Avatar answered Oct 26 '22 14:10

amit


Every thread has its own execution stack. This memory area contains all local variables and method parameters. When two threads execute the same code concurrently, both are using distinct stacks.

Thus it is not possible to change the value of a method parameter or local variable by a different thread.

like image 44
Tomasz Nurkiewicz Avatar answered Oct 26 '22 13:10

Tomasz Nurkiewicz