这两个类都是非public类,也就是保留类,仅能被同一个包中的代码所使用
DefaultFileSystem类没有任何的继承关系,也未引入任何包(甚至连类头注释都没有)
代码如下(就一个方法,不存在变量
public static FileSystem getFileSystem() { return new UnixFileSystem(); }
作用是返回默认的Unix文件系统对象(默认构造方法给的是默认对象)
DeleteOnExitHook类引入了以下包
import java.util.*; import java.io.File;
不存在继承关系
类头注释如下:
/** * This class holds a set of filenames to be deleted on VM exit through a shutdown hook. * A set is used both to prevent double-insertion of the same file as well as offer * quick removal. */
大意为:
这个类记录了在虚拟机关闭时将被删除的文件名,通过关闭触发(删除)
被用于防止文件的重复插入和提供快速删除功能
该类含有一个成员变量:
记录文件名的具有可预知迭代顺序的 Set 接口的哈希表和链接列表实现
private static LinkedHashSet<String> files = new LinkedHashSet<>();
含有三个方法:
静态构造方法(默认
private DeleteOnExitHook() {}
向链接列表中添加文件
static synchronized void add(String file) { if(files == null) { // DeleteOnExitHook is running. Too late to add a file throw new IllegalStateException("Shutdown in progress"); } files.add(file); }
进行删除工作
static void runHooks() { LinkedHashSet<String> theFiles; synchronized (DeleteOnExitHook.class) {//数据绑定 theFiles = files; files = null;//清空该类的记录 } ArrayList<String> toBeDeleted = new ArrayList<>(theFiles); // reverse the list to maintain previous jdk deletion order. // Last in first deleted. Collections.reverse(toBeDeleted);//反转排列顺序(反转后类似栈,后添加先删除) for (String filename : toBeDeleted) { (new File(filename)).delete(); } }
含有一个静态代码块:检测该类的执行触发条件
static { // DeleteOnExitHook must be the last shutdown hook to be invoked. // Application shutdown hooks may add the first file to the // delete on exit list and cause the DeleteOnExitHook to be // registered during shutdown in progress. So set the // registerShutdownInProgress parameter to true. sun.misc.SharedSecrets.getJavaLangAccess() .registerShutdownHook(2 /* Shutdown hook invocation order */, true /* register even if shutdown in progress */, new Runnable() { public void run() { runHooks(); } } ); }
这两个类都比较简单,同时也是保留类,大概看下即可