I believed the String pool abandoned local Strings when their methods completed
Yet:
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();
}
}
According to the whizlabs' guide, this code is correcly synchronizing its threads based on a local String. The output will always be AABB or BBAA. Why? Why does the str String outlive its local declaration?