4

I'm new to Java, so pls excuse if answer to below simple case is obvious.

class A{
   public void foo(Customer cust){
       cust.setName(cust.getFirstName() + " " + cust.getLastName());
       cust.setAddress(new Address("Rome"));
   }
}

I've a Singleton object (objectA) created for class A.

  1. Given I don't have any class variable, is it thread safe if I call objectA.foo(new Customer()) from different threads?

  2. What if I change foo to static and call A.foo(new Customer()) from different threads? is it still thread safe?

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
Aymer
  • 321
  • 1
  • 4
  • 11
  • 3
    Making the method `synchronized` does not automatically make it thread-safe. In fact, in this example, it wouldn't do anything useful. – Jesper Jan 08 '14 at 15:26

3 Answers3

5

Given I don't have any class variable, is it thread safe if I call objectA.foo(new Customer()) from different threads?

Of course it is. Your foo() method doesn't change any state of the A object (since it doesn't have any) and the object you pass, new Customer(), as an argument to the method is not available to any other thread.

What if I change foo to static and call A.foo(new Customer()) from different threads? is it still thread safe?

As long as you don't have any mutable static state, you're still good.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Probably the person with that gave you a down vote did not understand your answer. – Pete B. Jan 08 '14 at 15:31
  • Thanks for the answer, The premise I was asking this question was, http://stackoverflow.com/a/18547670/388889 , Not sure how accurate it is. But it says "if every object you pass in the o parameter is immutable" and here Customer is not immutable. Any hint? – Aymer Jan 08 '14 at 15:32
  • @Aymer the keyword here is **shared** mutable data. If the objects aren't shared between various threads, than, despite it being mutable, it is still thread-safe – John Vint Jan 08 '14 at 15:33
  • @Aymer Because you create the object (`new Customer()`) directly in the method invocation, there's no chance of a reference to it leaking to any other threads. – Sotirios Delimanolis Jan 08 '14 at 15:36
  • @SotiriosDelimanolis & John, Thanks for the clarification. On a side note, never expected this kind of super fast responses! – Aymer Jan 08 '14 at 15:40
3

Yes, it will be thread-safe IF you call foo(new Customer()) from different threads. But this is only because each time you call new Customer() you are making a new (and therefore different) Customer object, and all that foo does is alter the state of the Customer that is passed to it. Thus these threads will not collide, because even though they are calling the same method, they will be manipulating different customers.

However, if you were to create a customer variable first

Customer bob = new Customer()

and then call foo(bob) from two different threads, it would not be thread safe. The first thread could be changing the address while the second thread is changing the name, causing inconsistent behavior and / or corrupt data.

If you want to make this method truly thread-safe, just declare the method synchronized:

public synchronized void foo(Customer cust) {...}
James Dunn
  • 8,064
  • 13
  • 53
  • 87
1

thread safety is required where a function is accessing a static shared variable. like a function which is updating a shared document, so if two thread in parallel updated changes of one thread will get ignore. Or a static variable which is shared across the application, singleton object.

Above are some situation where thread safety required In your case you are not updating any shared resource so this is a thread safe.

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110