使用本地对象引用进行堆栈限制 [英] Stack Confinement using local object reference

查看:46
本文介绍了使用本地对象引用进行堆栈限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循的是 Java Concurrency in Practice ,当我阅读有关堆栈限制的文章时,我感到很好,因为它解释得很好,但是此声明对我提出了一些疑问:

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();
    }

}

推荐答案

此声明对我提出了一些疑问:

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?

当然.如果这不是问题,那么Goetz等.首先不会花时间在堆栈限制上.

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.

从字面上看,可以在您的特定代码中引入堆栈约束违规的方式是无穷的,但是如何做到这一点:假设我想让 StackConfinement.setSomeMoreProperty()实际上具有持久性效果,例如将生成的 Person 对象放入 List .看起来像这样:

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);
    }

    // ...
}

这很好,但是现在 people 成员及其引用的任何对象(例如 setSomeMoreProperty()添加的 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.

这篇关于使用本地对象引用进行堆栈限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆