1

I am having a code like below in my class that will act like my 'Task' class. i will create multiple instances of this class and pass it to the threadpool

class Task {

    public void doJob() {

        String s = informationDto.getName();

    }
}

DTO:

class InformationDto{

    private String name;

    public getName(){}

    public setName(String n){}

}

i am wondering about the informationDto.getName(); statement. will it be return me the correct name always in a multithreaded environment?

what happens if the getName() method is static?

i want to use informationDto each for every Task class i am creating. what will be the best way to do this?

1) i want to keep one InformationDto for each Task class. 2) since instance variables are not thread safe i am wondering if i access getName() method (since name is a instance variable ) would i get the correct name that i set in the Task class? 3) it is like if i use a util class inside the Task class will it be thread safe? if not how can i use kind of util instance in a thread safe manner?

mleko
  • 11,650
  • 6
  • 50
  • 71
Sam
  • 2,055
  • 6
  • 31
  • 48
  • 1
    what do you mean when you say "correct name"? will someone be calling `setName()` on the same instance in parallel? do you need the `InformationDTO` to be immutable? there are several answers that might fit your question so maybe give it a little more background? – Pavel Veller May 08 '12 at 02:22
  • `InformationDto` is not a immutable class. it is an ordinary DTO. so how i can use that DTO in a thread safe manner? meaning that since i am using it in a method variable inside the `Task` class will it be thread safe? since i am accessing `DTO`s instance variables inside the `doJob` method. – Sam May 08 '12 at 02:35
  • Thread-safe means you don't get unexpected concurrent modifications. something like iterating a collection while another thread modifies it. In you case it looks like the `getName()` returns some internal state while `setName()` can change it. You won't get a partial read of that state in the middle of someone updating it (like having half the new string and half the old string) but your reads can change between threads if someone is writing to it using the `setName()`. hence my question. What behavior do you expect or don't want to see? define what thread-safe means for you in this context – Pavel Veller May 08 '12 at 02:41
  • @PavelVeller Thanks a lot for the explanation. i updated my question.hope it will explain you about my question. – Sam May 08 '12 at 02:56
  • @Sam Check this out: http://stackoverflow.com/questions/9851133/when-to-use-volatile-and-synchronized – Jeremy May 08 '12 at 03:24

1 Answers1

4

I am still not exactly clear about your problem but enough said in comments, will try to give you an answer.

I would assume the getName() does this:

public getName() {
    return name;
}

and setName() does this:

public setName(String name) {
    this.name = name;
}

where name is:

private String name;

Having an instance of this class used by multiple threads does not guarantee that everyone will see the same thing when they ask getName(), not until it's guaranteed that no one calls setName() on it while the others are working.

One way to guarantee the getName() to return a consistent and the same value is to lock it down. You do so by making your object immutable:

private final name;

public InformationDTO(String name) {
  this.name = name;
}

This way name is guaranteed not to change and you can pass your object safely to as many threads as you wants knowing that no one can change it. This will however make it not a perfect POJO in a way that you can't set that property. If you need to be able to use setName() (directly or indirectly through some kind of a framework) this won't do it for you.

What else can go wrong with this instance in a multithreaded environment? not much. The getName() won't return a partial state, like half the old value and half the new value being written by another thread. You're safe there. Making the getName() static doesn't change much. It will now be a class method, not an instance method with the name also becoming static which is very much a global state tat you don't want in your backyard.

Hope this answers your question or at least gives you enough clues to figure out your next steps.

UPDATE. Like it was pointed out in comments, if you do expect the name to change and want to be sure all threads read the most recent value, you can use volatile when declaring the member variable:

private volatile String name;

Using volatile will ensure your threads "communicate" on modification events and don't trust their local caches.

Pavel Veller
  • 6,085
  • 1
  • 26
  • 24
  • It's not guaranteed that any new value of `name` will be seen by any other thread unless the field is marked volatile or uses some other form of synchronization. – Jeremy May 08 '12 at 03:19
  • 1
    right. it just sounded like @Sam was looking for some more basic advises around dealing with concurrency. anyway. will go update the answer to be more complete. Thanks – Pavel Veller May 08 '12 at 03:22
  • @PavelVeller - Thank you very much for your answer. so it seems like i will go through with the `constructor` injection rather than using the `setter` method. your answer cleared that out for me. thanks. – Sam May 08 '12 at 04:34