Concurrently invoking Java method of singleton object Concurrently invoking Java method of singleton object multithreading multithreading

Concurrently invoking Java method of singleton object


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.


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.


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.