를 사용하여, 나는 스택 감금에 대해 읽을 때 나는 좋은 느낌이 아주 잘 설명하기 때문에, 그러나이 문장은 나를 위해 몇 가지 의문이 제기 :스택 감금는 연습</em>에 내가 <em>자바 동시성 다음하고 지역의 객체 참조
을 객체 참조에 대한 유지 보수 스택 제한은 지시 대상이 탈출하지 않도록하기 위해 프로그래머는 조금 추가 지원을 필요로
사람이 O를 위반을 생산하기 위해 아래의 코드에서 뭔가를 할 수 f stack 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();
}
}
코드에 people.add (person) 행을 추가 할 때 발생할 수있는 문제의 유형을 알려주십시오. 그 객체가 올바르게 추가되지는 않지만 그 밖의 다른 것을 볼 수 있습니까? –
@so_what, 그 메소드 (성공하면)는 당신이 물었던'person'에 대해 스택 감금을 위반하게 만든다. 'person'이 참조하는 객체가 로컬 변수가 아닌'StackConfinement' 객체의'people' 멤버를 통해 접근 할 수 있기 때문에 그 효과가 있습니다. 나는 그 점에 대한 답을 분명히했다. –
네, 이제 사람 개체가 목록에 추가되었으므로이 개체는 다른 스레드에서 나중에 수정할 수 있으므로 제한되지 않습니다.이 목록이 내가 원하는 최종 출력 인 경우 문제가 발생하지 않습니다. 답을 고맙습니다. –