Java 引用类型及常见应用

引用的类型主要关注的是该引用如何与GC交互。

1.Strong References

java中常见的引用类型:

StringBuffer buf = new StringBuffer();

这里buf就是一个强引用。如果一个对象在一条强引用链上可达,那么它不适合GC,也不会被回收。

2.Weak References

弱引用,是一种较弱的引用,它不会阻止GC回收只被弱引用引用的对象。

当一个对象最强只被弱引用引用时,那么在下个GC周期,它可以被回收.

你可以使用如下的方式构建一个弱引用:

WeakReference<Widget> weakWidget = new WeakReference<Widget>(widget);

在其他地方你可以使用如下的代码来获取原始对象:

weakWidget.get()

但是你可能会得到一个NULL值。

使用到弱引用的常见类是:

ThreadLocal类:

java.util.WeakHashMap

3.Soft References

软引用,与弱引用类似,弱引用在下一个GC周期就会被回收,但是软引用一般会坚持更久.

更常见的,软引用,只要在内存够用的情况下就会一直存在.

那么它非常适合实现缓存.

下图是jdk中的继承关系图,可以看见很多跟缓存相关.

更多可以参考Google guava:

com.google.common.collect.MapMakerInternalMap

4.Phantom References

幻象引用,一种更弱的引用,最弱的,它的get()方法总是返回NULL.

它一般用于在对象GC时得到一个通知,通知你可以做一些别的事情,

在apache的commons-fileupload 和commons-io中很巧妙的使用了这个类,来实现上传时临时文件的清除.

在commons-fileupload中每个文件都指定了一个文件清除的跟踪器.

org.apache.commons.fileupload.disk.DiskFileItemFactory.fileCleaningTracker

而commons-io中实现,最重要的在:Reaper线程的run方法中.(删除了部分代码.)

package org.apache.commons.io;

/**
 * Keeps track of files awaiting deletion, and deletes them when an associated
 * marker object is reclaimed by the garbage collector.
 * <p>
 * This utility creates a background thread to handle file deletion.
 * Each file to be deleted is registered with a handler object.
 * When the handler object is garbage collected, the file is deleted.
 * <p>
 * In an environment with multiple class loaders (a servlet container, for
 * example), you should consider stopping the background thread if it is no
 * longer needed. This is done by invoking the method
 */
public class FileCleaningTracker {

    // Note: fields are package protected to allow use by test cases

    /**
     * Queue of <code>Tracker</code> instances being watched.
     */
    ReferenceQueue<Object> q = new ReferenceQueue<Object>();
    /**
     * Collection of <code>Tracker</code> instances in existence.
     */
    final Collection<Tracker> trackers = Collections.synchronizedSet(new HashSet<Tracker>()); // synchronized
    /**
     * Whether to terminate the thread when the tracking is complete.
     */
    volatile boolean exitWhenFinished = false;
    /**
     * The thread that will clean up registered files.
     */
    Thread reaper;

    /**
     * Track the specified file, using the provided marker, deleting the file
     * when the marker instance is garbage collected.
     * The speified deletion strategy is used.
     *
     * @param path  the full path to the file to be tracked, not null
     * @param marker  the marker object used to track the file, not null
     * @param deleteStrategy  the strategy to delete the file, null means normal
     * @throws NullPointerException if the path is null
     */
    public void track(final String path, final Object marker, final FileDeleteStrategy deleteStrategy) {
        if (path == null) {
            throw new NullPointerException("The path must not be null");
        }
        addTracker(path, marker, deleteStrategy);
    }

    /**
     * Adds a tracker to the list of trackers.
     *
     * @param path  the full path to the file to be tracked, not null
     * @param marker  the marker object used to track the file, not null
     * @param deleteStrategy  the strategy to delete the file, null means normal
     */
    private synchronized void addTracker(final String path, final Object marker, final FileDeleteStrategy
            deleteStrategy) {
        // synchronized block protects reaper
        if (exitWhenFinished) {
            throw new IllegalStateException("No new trackers can be added once exitWhenFinished() is called");
        }
        if (reaper == null) {
            reaper = new Reaper();
            reaper.start();
        }
        trackers.add(new Tracker(path, deleteStrategy, marker, q));
    }

    //-----------------------------------------------------------------------
    /**
     * The reaper thread.
     */
    private final class Reaper extends Thread {
        /** Construct a new Reaper */
        Reaper() {
            super("File Reaper");
            setPriority(Thread.MAX_PRIORITY);
            setDaemon(true);
        }

        /**
         * Run the reaper thread that will delete files as their associated
         * marker objects are reclaimed by the garbage collector.
         */
        @Override
        public void run() {
            // thread exits when exitWhenFinished is true and there are no more tracked objects
            while (exitWhenFinished == false || trackers.size() > 0) {
                try {
                    // Wait for a tracker to remove.
                    final Tracker tracker = (Tracker) q.remove(); // cannot return null
                    trackers.remove(tracker);
                    if (!tracker.delete()) {
                        deleteFailures.add(tracker.getPath());
                    }
                    tracker.clear();
                } catch (final InterruptedException e) {
                    continue;
                }
            }
        }
    }

    //-----------------------------------------------------------------------
    /**
     * Inner class which acts as the reference for a file pending deletion.
     */
    private static final class Tracker extends PhantomReference<Object> {

        /**
         * The full path to the file being tracked.
         */
        private final String path;
        /**
         * The strategy for deleting files.
         */
        private final FileDeleteStrategy deleteStrategy;

        /**
         * Constructs an instance of this class from the supplied parameters.
         *
         * @param path  the full path to the file to be tracked, not null
         * @param deleteStrategy  the strategy to delete the file, null means normal
         * @param marker  the marker object used to track the file, not null
         * @param queue  the queue on to which the tracker will be pushed, not null
         */
        Tracker(final String path, final FileDeleteStrategy deleteStrategy, final Object marker,
                final ReferenceQueue<? super Object> queue) {
            super(marker, queue);
            this.path = path;
            this.deleteStrategy = deleteStrategy == null ? FileDeleteStrategy.NORMAL : deleteStrategy;
        }

        /**
         * Deletes the file associated with this tracker instance.
         *
         * @return {@code true} if the file was deleted successfully;
         *         {@code false} otherwise.
         */
        public boolean delete() {
            return deleteStrategy.deleteQuietly(new File(path));
        }
    }

}
时间: 2024-08-06 03:40:38

Java 引用类型及常见应用的相关文章

Java引用类型详解

JVM  的垃圾回收器对于不同类型的引用有不同的处理方式.java中对于一个对象来说,只要有引用的存在,它就会一直存在于内存中.如果这样的对象越来越多,超出了JVM中的内存总数,JVM就会抛出OutOfMemory错误.虽然垃圾回收的具体运行是由JVM来控制的,但是开发人员仍然可以在一定程度上与垃圾回收器进行交互,其目的在于更好的帮助垃圾回收器管理好应用的内存.这种交互方式就是使用JDK1.2 引入的  java.lang.ref包. 强引用(strong reference) 在一般的 Jav

java中不常见的keyword:strictfp,transient

1.strictfp, 即 strict float point (精确浮点). strictfp keyword可应用于类.接口或方法.使用 strictfp keyword声明一个方法时,该方法中全部的float和double表达式都严格遵守FP-strict的限制,符合IEEE-754规范.当对一个类或接口使用 strictfp keyword时,该类中的全部代码,包含嵌套类型中的初始设定值和代码,都将严格地进行计算.严格约束意味着全部表达式的结果都必须是 IEEE 754 算法对操作数预

Java 中最常见的 5 个错误

在编程时,开发者经常会遭遇各式各样莫名错误.近日,Sushil Das在 Geek On Java上列举了 Java 开发中常见的 5 个错误,与君共「免」. 1.Null 的过度使用 避免过度使用 null 值是一个最佳实践.例如,更好的做法是让方法返回空的 array 或者 collection 而不是 null 值,因为这样可以防止程序抛出 NullPointerException.下面代码片段会从另一个方法获得一个集合: List<String> accountIds = person

java 引用类型及作用

java 引用类型 0. 引言 Java 中一共有 4 种类型的引用 : StrongReference. SoftReference. WeakReference 以及 PhantomReference , 这 4 种类型的引用与 GC 有着密切的关系. 1. 强引用 (StrongReference) 普通的引用做法,如: String str = "hello"; 只要引用存在就不会被回收,除非手动置为null,或者超出范围,gc才会回收 2. 软引用 (SoftReferenc

深入理解Java引用类型

深入理解Java引用类型 在Java中类型可分为两大类:值类型与引用类型.值类型就是基本数据类型(如int ,double 等),而引用类型,是指除了基本的变量类型之外的所有类型(如通过 class 定义的类型).所有的类型在内存中都会分配一定的存储空间(形参在使用的时候也会分配存储空间,方法调用完成之后,这块存储空间自动消失), 基本的变量类型只有一块存储空间(分配在stack中), 而引用类型有两块存储空间(一块在stack中,一块在heap中),在函数调用时Java是传值还是传引用,这个估

用Java来写常见的排序算法

随着校招的临近 算法是校招中很重要的一个部分 总结了常见几种排序算法,各种算法的时间复杂度和空间复杂度大家也需要多了解下 package com.huwei.sort; /** * 各种排序算法 * * @author huwei * */ public class Sort { public static void main(String[] args) { int[] a = { 60, 57, 89, 47, 57, 98, 45, 35, 73 }; Sort sort = new So

【转】Java 枚举7常见种用法

原文网址:http://softbeta.iteye.com/blog/1185573 Java 枚举7常见种用法 博客分类: java java枚举enmu 原创地址:http://blog.lichengwu.cn/java/2011/09/26/the-usage-of-enum-in-java/ JDK1.5引入了新的类型——枚举.在 Java 中它虽然算个“小”功能,却给我的开发带来了“大”方便. 用法一:常量 在JDK1.5 之前,我们定义常量都是: public static fi

day11&lt;Java开发工具&amp;常见对象&gt;

Java开发工具(常见开发工具介绍) Java开发工具(Eclipse中HelloWorld案例以及汉化) Java开发工具(Eclipse的视窗和视图概述) Java开发工具(Eclipse工作空间的基本配置) Java开发工具(Eclipse中内容辅助键的使用) Java开发工具(Eclipse中快捷键的使用) Java开发工具(Eclipse中如何提高开发效率) Java开发工具(Eclipse中一个标准学生类及其测试) Java开发工具(Eclipse中接口抽象类具体类代码体现) Jav

Java web中常见编码乱码问题(二)

根据上篇记录Java web中常见编码乱码问题(一), 接着记录乱码案例: 案例分析:   2.输出流写入内容或者输入流读取内容时乱码(内容中有中文) 原因分析: a. 如果是按字节写入或读取时乱码,应该先检测系统编码是什么样的,看看是否支持中文编码的字符集. System.out.println(System.getProperty("file.encoding")); a1.如果是不支持中文的就要设置jvm编码了,设置方法: Windows环境就在catalina.bat配置: s