1

I am following Java Concurrency in Practice, and when I read about stack confinement I felt good because it explained very well, but this statement raised some doubts for me:

Maintaining stack confinement for object references requires a little more assistance from the programmer to ensure that the referent does not escape

Can anyone do something in below code to produce a violation of stack confinement? I guess it has no confinement violation at present. I want to know how local object reference can violate the confinement.

/**
 * 
 */
package lession2.shared.object;

/**
 * @author so_what
 */
class Person {
    private String personName;
    private String personAddress;

    public String getPersonName() {
        return personName;
    }

    public void setPersonName(String personName) {
        this.personName = personName;
    }

    public String getPersonAddress() {
        return personAddress;
    }

    public void setPersonAddress(String personAddress) {
        this.personAddress = personAddress;
    }

    @Override
    public String toString() {
        return "Person [personName=" + personName + ", personAddress=" + personAddress + "]";
    }
}

public class StackConfinement extends Thread {

    public void setSomeMoreProperty() {
        //this person object will be confined to each thread
        Person person=new Person();
        person.setPersonAddress("NY");
        person.setPersonName("Xyz");
        //now I wan to pass this person to the other method
        doSomething(person);
        System.out.println(person);
    }

    public void doSomething(Person person) {
        person.setPersonAddress("Canada");
        //can one add some thing here and violate the thread confinement
    }   

    @Override
    public void run()
    {
        setSomeMoreProperty();
    }

    public static void main(String[] args) throws InterruptedException {
        StackConfinement thread1=new StackConfinement();
        StackConfinement thread2=new StackConfinement();
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
    }

}
John Bollinger
  • 160,171
  • 8
  • 81
  • 157
so_what
  • 176
  • 10

1 Answers1

3

this statement raised some doubts for me:

Maintaining stack confinement for object references requires a little more assistance from the programmer to ensure that the referent does not escape

Can anyone do something in below code to produce a violation of stack confinement?

Of course. If this were not a thing then Goetz et al. would not have spent time on stack confinement in the first place.

I guess it has no confinement violation at present. I want to know how local object reference can violate the confinement.

A reference stored in a local variable does not violate stack confinement, which the book defines as the situation in which an object can be reached only through local variables. The problem arises when there is a(nother) reference to the object that is more broadly reachable. That would happen, for example, if you store a reference to object in a static field of any class. It also happens if you store a reference to the object in a container that is not itself stack confined.

The book gives the example of a more subtle case in which a reference is stored in a container that itself is initially stack-confined, but later is published. Since the object is reachable from the (no longer stack-confined) container, it is no longer stack-confined either.

There are literally an infinity of ways in which stack-confinement violations could be introduced into your particular code, but how about this: suppose I wanted to make StackConfinement.setSomeMoreProperty() actually have a persistent effect, such as putting the Person objects it generates into a List. That would look like this:

public class StackConfinement extends Thread {

    private List<Person> people = new ArrayList<>();

    public void setSomeMoreProperty() {
        // Initially stack-confined
        Person person = new Person();

        person.setPersonAddress("NY");
        person.setPersonName("Xyz");

        // does not break stack confinement:
        doSomething(person);
        System.out.println(person);

        // this DOES break stack confinement:
        people.add(person);
    }

    // ...
}

That's all well and good, but now neither the people member nor any object it references (such as the person that is added by setSomeMoreProperty()) is stack-confined.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • In your code when you add a line people.add(person) can you please tell what type of problem that can cause.One problem I can see that object will not added properly but what else ? – so_what Dec 21 '17 at 21:47
  • @so_what, that method (if it succeeds) causes stack confinement to be violated for the `person`, which was what you asked about. It has that effect because it causes the object to which `person` refers to be accessible also via the host `StackConfinement` object's `people` member, which is not a local variable. I have clarified the answer in that regard. – John Bollinger Dec 21 '17 at 22:18
  • yes now because people object is added to the list then this object can be modified later by any-other thread so its not confined.So confinement will not cause any issue if this list is the final output I want.Thanks for the answer – so_what Dec 22 '17 at 11:40