zoukankan      html  css  js  c++  java
  • java的几种引用之二

    import java.lang.ref.PhantomReference;
    import java.lang.ref.ReferenceQueue;
    import java.lang.ref.SoftReference;
    import java.lang.ref.WeakReference;
    import java.util.Map;
    import java.util.WeakHashMap;

    public class Ref {

    public Ref(){

    }

    public static void main(String[] args){
    try {
    // test1();
    // test2();
    // test3();
    // test4();
    // test5();
    test6();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }

    }


    /**
    * 强引用,JVM的默认实现
    * @throws InterruptedException
    */
    public static void test1()throws InterruptedException{
    Object obj = new Object();
    Object strong =obj;

    obj =null;
    //一个对象具有强引用垃圾机制不会回收它,当内存空 间不足,Java虚拟机宁愿抛出OutOfMemoryError错误,使程序异常终止,也不会靠随意回收具有强引用的对象来解决内存不足问题
    System.gc();
    Thread.sleep(1000);
    System.out.println("strong="+strong);
    }


    /**
    * WeakReference 弱引用 当所引用对象在JVM,内不再强引用是,GC后 weak reference 将会被自动回收
    * @throws InterruptedException
    */
    public static void test2() throws InterruptedException{
    Object obj = new Object();
    WeakReference<Object> wr = new WeakReference<Object>(obj);
    obj=null;
    System.gc();
    Thread.sleep(1000);
    System.out.println("wr.get()="+wr.get());
    System.out.println("wr="+wr);
    wr.clear();
    System.out.println("w111r="+wr.get());

    }

    /**
    * SoftReference 软引用 SoftReference 于 weakReference 的特性基本一致,最大区别在于
    * SoftReference 会尽可能长的保留引用直到JVM 内存不足时才会被回收(虚拟机保证)
    * @throws InterruptedException
    */
    public static void test3() throws InterruptedException{
    Object obj = new Object();
    SoftReference<Object> sr = new SoftReference<Object>(obj);
    obj=null;
    System.gc();
    Thread.sleep(1000);
    System.out.println("sr.get()="+sr.get());
    }

    /**
    * PhantomReference 虚引用 Phantom Reference 与 WreakReference 和 SoftReference 有
    * 很大的不同,因为get()方法永远返回null
    * @throws InterruptedException
    */
    public static void test4() throws InterruptedException{
    Object obj = new Object();
    ReferenceQueue<Object> rq = new ReferenceQueue<Object>();
    PhantomReference<Object> pr = new PhantomReference<Object>(obj, rq);
    System.out.println("pr.get()="+pr.get());
    }


    /**
    * ReferenceQueue
    * @throws InterruptedException
    */
    public static void test5() throws InterruptedException{

    Object obj = new Object();
    ReferenceQueue<Object> rq = new ReferenceQueue<Object>();
    WeakReference<Object> pr = new WeakReference<Object>(obj, rq);

    System.out.println("**pr.enqueue()="+pr.enqueue());
    System.out.println("**pr.isEnqueued()="+pr.isEnqueued());
    System.out.println("**pr="+pr);
    System.out.println("**rq.poll()="+rq.poll());
    obj=null;
    System.gc();

    System.out.println("pr.enqueue()="+pr.enqueue());
    System.out.println("**pr.isEnqueued()="+pr.isEnqueued());
    System.out.println("pr="+pr);
    System.out.println("rq.poll()="+rq.poll());
    System.out.println("obj5="+obj);
    }



    /**
    * 使用WeakReference 作为key,一旦没有指向key 的强引用
    * weakHashMap 在gc 后将自动删除相关的
    * @throws InterruptedException
    */
    public static void test6()throws InterruptedException{

    Map<Object,Object> map = new WeakHashMap<Object,Object>();
    Object key = new Object();
    Object value = new Object();
    map.put(key,value);
    key =null;


    System.out.println("value="+value);
    System.out.println("key="+key);
    System.out.println("map.containsValue(value)="+map.containsValue(value));
    System.out.println("map="+map);

    System.gc();
    Thread.sleep(1000);


    System.out.println("value="+value);
    System.out.println("key="+key);
    System.out.println("map.containsValue(value)="+map.containsValue(value));
    System.out.println("map="+map);

    }
    }转

  • 相关阅读:
    location url 反向代理到来机的其它端口 gitlab
    PortSentry是入侵检测工具中配置最简单、效果最直接的工具之一
    iptables下state的4种形式
    windows  远程桌面命令 mstsc
    linux中解决SSH连接慢问题 关键点GSSAPIAuthentication
    无密码登录远程主机
    openfire 服务器名称:后面的黄色叹号
    ssh -v root@xxxxx 显示登录的细节
    mysql 只导数据不含表结构
    磁盘空间占满inode结点没用完 并删除了文件但是释放不了
  • 原文地址:https://www.cnblogs.com/wangruihua-521/p/3159024.html
Copyright © 2011-2022 走看看