Martin Asenov
Martin Asenov

Reputation: 1298

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!

Upvotes: 10

Views: 3358

Answers (5)

vladimir e.
vladimir e.

Reputation: 723

What the others said. Though to nit-pick: in java only local primitive variables and parameters (int, long, boolean etc.) are allocated on the stack all other objects are allocated on the heap and only references are stored on the stack. But the others said each invocation of the method will see its own copy of local variables.

Note that objects (as opposed to primitives) passed as parameters are not necessarily unique to each invocation, since objet parameters are just references. So if you pass the same input object to the method twice then both will be operating on the same input.

Upvotes: 3

Tudor
Tudor

Reputation: 62439

A method is essentially a piece of code. When a group of threads call a method, a distinct copy of the method exists on each thread's stack, so local variables do not interfere with eachother.

If additionally, all parameters are distinct for each thread, then there is perfect isolation, unless your method is operating on additional shared data inside its code.

Upvotes: 3

JB Nizet
JB Nizet

Reputation: 691765

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.

Upvotes: 13

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340743

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.

Upvotes: 3

amit
amit

Reputation: 178461

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.

Upvotes: 8

Related Questions