[code="java"]public class CommonPoolTest2
{
private static final String x="ccc";
public static String getX()
{
return x;
}
}[/code]
[code="java"]
public class CommonPoolTest
{
public static void main(String[] args) throws IOException
{
String strc = new String("ccc").intern();
CommonPoolTest2 cpt2= new CommonPoolTest2();
System.out.println(strc==cpt2.getX());
}
}[/code]
结果是true
那就是说常量池是所有jvm中类共享的,那么当多个线程同时执行intern的时候,为了线程安全,是不是要锁住整个常量池,如果这样,当大量并发线程的情况下,会不会导致性能很低?还是采用的其他更灵活的方式呢,谢谢!
[b]问题补充:[/b]
[quote]该池的粒度比较粗,如果驻留的字符串过多倒是可能引发更多的hash冲突而使得池的性能降低。[/quote]
有什么办法能降低同步的开销吗,或者降低锁的粒度?