String 풀이 메소드가 완료되면 로컬 문자열을 버렸다고 믿었습니다.문자열 풀이 지역 변수를 사용합니까?
그러나
public class TestPool implements Runnable{
/**
* @param args the command line arguments
*/
public void run() {
String str= "hello";
synchronized(str){
try {
System.out.print(Thread.currentThread().getName());
Thread.sleep(500);
System.out.print(Thread.currentThread().getName());
}
catch(InterruptedException e){
}
}
}
public static void main(String []args){
new Thread(new TestPool(),"A").start();
new Thread(new TestPool(),"B").start();
}
}
whizlabs 가이드에 따르면이 코드는 로컬 문자열을 기준으로 해당 스레드를 동기화하고 있습니다. 출력은 항상 AABB 또는 BBAA입니다. 왜? str String이 지역 선언보다 오래 지속되는 이유는 무엇입니까?
+1 "(업데이트 중) - 바로 돌아 오십시오!" –
나는 그것을 더 자주 써야만한다 :) 나의 마지막 텍스트가 또한 투표에 도움이되기를 희망한다. –
참조가 로컬 변수이지만 가리키는 String이 아닙니다. 선언을 String str = new ("hello")로 변경할 때 어떤 일이 일어나는 지보십시오. – andandandand