AutoCloseable接口源码理解

35 篇文章 0 订阅
34 篇文章 0 订阅


前言

本文章帮助大家对AutoCloseable接口的理解。


一、概述

AutoCloseable意为可自动关闭的。顾名思义,为在try-with-resources语句中使用的“资源”对象(如文件、套接字句柄等)可在块结束后自动关闭,需要实现此接口。

重点try-with-resources语句如:try(/*在此使用可自动关闭的资源对象,多个对象用分号隔开...*/) { /*...*/ } catch ...,其中的“资源对象”需要实现此接口。

二、源码理解

package java.lang;

使用AutoCloseable接口时,此包自动引入。

AutoCloseable接口

public interface AutoCloseable { /*...*/ }

注意:只有实现此接口才能在try-with-resources语句中起自动关闭的效果。若语句中使用的是实现了其它具有close方法的接口,编译时将报“不兼容的类型”的异常,异常信息如下(中文版本):

java: 不兼容的类型: try-with-resources 不适用于变量类型
    (MyResource无法转换为java.lang.AutoCloseable)

使用例子如下,程序最终将打印三个“0”,即三个资源都会自动调用其close方法:

class MyResource implements AutoCloseable {
	@Override
	public void close() throws Exception {
		System.out.println(0);
		/*关闭资源*/
	}
}

MyResource resource1 = new MyResource();
try (resource1; MyResource resource2 = new MyResource(); MyResource resource3 = new MyResource()) {
	/*...*/
}
catch (Exception e) {
	/*...*/
}

编译源码参考:

  • Attr.java的public void visitTry(JCTry tree)(访问“try”语句)的boolean isTryWithResource = tree.resources.nonEmpty()(带资源的“try”(简称“twr”))、CheckContext twrContext = new Check.NestedCheckContext(resultInfo.checkContext)(twr上下文)的chk.basicHandler.report(pos, diags.fragment(Fragments.TryNotApplicableToType(details)))(twr上下文的重写报告方法,调用basicHandler的报告方法。带“不适用的变量类型”信息)、twrResult.check(resource, resource.type);(检查)、return chk.checkType(pos, found, pt, checkContext)
  • Check.java的if (checkContext.compatible(found, req, checkContext.checkWarner(pos, found, req)))(类型可比较)、checkContext.report(pos, diags.fragment(Fragments.InconvertibleTypes(found, req)))(不可比较时,调用twr上下文的报告方法。带“无法转换为”信息)、CheckContext basicHandler = new CheckContext()public void report(DiagnosticPosition pos, JCDiagnostic details)basicHandler的报告方法)的log.error(pos, Errors.ProbFoundReq(details))(打印“不兼容的类型”和其它错误信息);
  • Symtab.java的autoCloseableType = enterClass("java.lang.AutoCloseable")
  • CompilerProperties.java的public static Fragment ProbFoundReq(JCDiagnostic arg0)return new Fragment("compiler", "prob.found.req", arg0)(不兼容的类型)、public static Fragment TryNotApplicableToType(JCDiagnostic arg0)return new Fragment("compiler", "try.not.applicable.to.type", arg0);(不适用于变量类型)、public static Fragment InconvertibleTypes(Type arg0, Type arg1)return new Fragment("compiler", "inconvertible.types", arg0, arg1);(无法转换为)。

AutoCloseable方法

close

void close() throws Exception;

重点:关闭此对象所持资源,若资源不能被关闭,抛出Exception异常。重写此方法以自定义关闭资源方法。此方法在try-with-resources语句头中的对象上块结束后自动调用。

注意close方法抛出的异常也能被catch语句捕获到,并且其异常会被抑制。实现类不能抛InterruptedException异常,因为中断异常被抑制的话可能导致运行时不当行为。建议实现方法时考虑调用此方法具有幂等性,即多次调用没有其他副作用。


总结

新人源码理解,望大家多多指点。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值