Java WeakHashMap为什么不删除WeakReferenced对象

Java WeakHashMap为什么不删除WeakReferenced对象,java,collections,weakhashmap,Java,Collections,Weakhashmap,我最近试图理解java.util.WeakHashMap 但是当我使用WeakReference包装字符串时,WeakHash并没有完成条目 还请注意,我正在清除主线程中的WeakReference,然后才在thread方法中引用它 执行时,while循环根本没有中断 public class WeakHashMapTotorial { private static Map<String, String> map; public static void main(S

我最近试图理解java.util.WeakHashMap

但是当我使用WeakReference包装字符串时,WeakHash并没有完成条目

还请注意,我正在清除主线程中的WeakReference,然后才在thread方法中引用它

执行时,while循环根本没有中断

public class WeakHashMapTotorial
{
    private static Map<String, String> map;

    public static void main(String args[])
    {
        WeakReference<String> s = new WeakReference<String>("Maine");
        map = new WeakHashMap<>();
        map.put(s.get(), "Augusta");

        Runnable runner = new Runnable()
        {
            public void run()
            {
                while (map.containsKey("Maine"))
                {
                    try
                    {
                        Thread.sleep(1000);
                    } catch (InterruptedException ignored)
                    {}
                    System.out.println("Thread waiting");
                    System.gc();
                }
            }
        };
        Thread t = new Thread(runner);
        t.start();
        System.out.println("Main waiting");
        try
        {
            t.join();
        } catch (InterruptedException ignored)
        {}
        s.clear();

    }
}
公共类WeakHashMapTotorial
{
私有静态地图;
公共静态void main(字符串参数[])
{
WeakReference s=新WeakReference(“缅因州”);
map=新的WeakHashMap();
map.put(s.get(),“Augusta”);
Runnable runner=新的Runnable()
{
公开募捐
{
而(缅因州地图)
{
尝试
{
睡眠(1000);
}捕获(InterruptedException被忽略)
{}
System.out.println(“线程等待”);
gc();
}
}
};
螺纹t=新螺纹(流道);
t、 start();
System.out.println(“主等待”);
尝试
{
t、 join();
}捕获(InterruptedException被忽略)
{}
s、 清除();
}
}

WeakReference
包装在字符串常量周围将不起作用。字符串常量被保留,这意味着引用永远不会消失。此外,代码在run方法中保留了对同一常量的引用,进一步保证了强引用的保留。

WeakReference
包装在字符串常量周围将不起作用。字符串常量被保留,这意味着引用永远不会消失。此外,代码在run方法中保留了对同一常量的引用,进一步保证了强引用的保留。

WeakReference
包装在字符串常量周围将不起作用。字符串常量被保留,这意味着引用永远不会消失。此外,代码在run方法中保留了对同一常量的引用,进一步保证了强引用的保留。

WeakReference
包装在字符串常量周围将不起作用。字符串常量被保留,这意味着引用永远不会消失。此外,您的代码在run方法中保留对同一常量的引用,从而进一步保证保留强引用。

您正在使用的字符串由内部字符串池保存,请尝试以下操作:

    WeakReference<String> s = new WeakReference<String>(new String("Maine"));
    map = new WeakHashMap<>();
    map.put(s.get(), "Augusta");
WeakReference s=新的WeakReference(新字符串(“缅因”);
map=新的WeakHashMap();
map.put(s.get(),“Augusta”);
这种实习效应在

此外,字符串文字总是引用类的同一实例 一串这是因为字符串文字,或者更一般地说,字符串 这些是常量表达式(§15.28)的值,是“内部的” 以便使用String.intern方法共享唯一实例


您正在使用的字符串正由内部字符串池保留,请尝试以下操作:

    WeakReference<String> s = new WeakReference<String>(new String("Maine"));
    map = new WeakHashMap<>();
    map.put(s.get(), "Augusta");
WeakReference s=新的WeakReference(新字符串(“缅因”);
map=新的WeakHashMap();
map.put(s.get(),“Augusta”);
这种实习效应在

此外,字符串文字总是引用类的同一实例 一串这是因为字符串文字,或者更一般地说,字符串 这些是常量表达式(§15.28)的值,是“内部的” 以便使用String.intern方法共享唯一实例


您正在使用的字符串正由内部字符串池保留,请尝试以下操作:

    WeakReference<String> s = new WeakReference<String>(new String("Maine"));
    map = new WeakHashMap<>();
    map.put(s.get(), "Augusta");
WeakReference s=新的WeakReference(新字符串(“缅因”);
map=新的WeakHashMap();
map.put(s.get(),“Augusta”);
这种实习效应在

此外,字符串文字总是引用类的同一实例 一串这是因为字符串文字,或者更一般地说,字符串 这些是常量表达式(§15.28)的值,是“内部的” 以便使用String.intern方法共享唯一实例


您正在使用的字符串正由内部字符串池保留,请尝试以下操作:

    WeakReference<String> s = new WeakReference<String>(new String("Maine"));
    map = new WeakHashMap<>();
    map.put(s.get(), "Augusta");
WeakReference s=新的WeakReference(新字符串(“缅因”);
map=新的WeakHashMap();
map.put(s.get(),“Augusta”);
这种实习效应在

此外,字符串文字总是引用类的同一实例 一串这是因为字符串文字,或者更一般地说,字符串 这些是常量表达式(§15.28)的值,是“内部的” 以便使用String.intern方法共享唯一实例


字符串常量始终处于内部状态。它是JLS所必需的。字符串常量始终处于内部。它是JLS所必需的。字符串常量始终处于内部。它是JLS所必需的。字符串常量始终处于内部。这是JLS要求的。谢谢Chris。我现在得到了缺失的部分:)谢谢克里斯。我现在得到了缺失的部分:)谢谢克里斯。我现在得到了缺失的部分:)谢谢克里斯。我现在得到了缺失的部分:)