【java高级程序设计】类库拓展【Autocloseable接口、Runtime类】

30 篇文章 1 订阅
16 篇文章 0 订阅

目录

1、 AutoCloseable接口

      1.1 AutoCloseable接口作用:

      1.2 AutoCloseable接口特点:

      1.3 一些编程例子:

            1.3.1 无异常尝试:

            1.3.2 抛出异常尝试(仍可执行close())

2、 Runtime类

      Runtime.maxMemory()

      Runtime.totalMemory()

      Runtime.freeMemory()


1、 AutoCloseable接口

应用:项目一般会访问各种资源(建立连接、业务操作、关闭资源)。

内配置一close()方法(incompleted)

public interface IMessage extends AutoCloseable{
	public void send(String msg);
	
}

class NetMessage implements IMessage {
	public NetMessage() {
		//只要实例化了对象就表示进行消息的发送【建立连接】
		System.out.println("【连接】");
	}

	@Override
	public void close() throws Exception {
		// AutoCloseable接口里含有的close()方法
		
	}

	@Override
	public void send(String msg) {
		
	}
}

      1.1 AutoCloseable接口作用:

自动关闭资源(资源访问有上限,需要释放资源)。

      1.2 AutoCloseable接口特点:

若有异常,仍可进行关闭。

      1.3 一些编程例子:

            1.3.1 无异常尝试:

package autocloseab;  
 
/** 
 * @author: pyu
 * @Date: 2021/09/09
 */

public interface IMessage extends AutoCloseable{
	public void send(String msg);
	
}

class NetMessage implements IMessage {
	public NetMessage() {
		//只要实例化了对象就表示进行消息的发送【建立连接】
		System.out.println("【连接】...");
	}

	@Override
	public void close() throws Exception {
		// AutoCloseable接口里含有的close()方法
		System.out.println("【关闭】...");
	}

	@Override
	public void send(String msg) {
		System.out.println("【发送】..." + msg);
	}
}
package autocloseab;  
 
/** 
 * @author: pyu
 * @Date: 2021/09/09
 */

public class MsgDemo {

	public static void main(String[] args) {
				NetMessage msg = new NetMessage();
				msg.send("pyu, have a nice day!");
	}

}

编译结果:

            1.3.2 抛出异常尝试(仍可执行close())

package autocloseab;  
 
/** 
 * @author: pyu
 * @Date: 2021/09/09
 */

public interface IMessage extends AutoCloseable{
	public void send(String msg);
	
}

class NetMessage implements IMessage {
	public NetMessage() {
		//只要实例化了对象就表示进行消息的发送【建立连接】
		System.out.println("【连接】...");
	}

	@Override
	public void close() throws Exception {
		// AutoCloseable接口里含有的close()方法
		System.out.println("【关闭】...");
	}

	@Override
	public void send(String msg) {
		if(msg.contains("pyu")) {
			throw new RuntimeException("pyu:runtimeException");
		}
		System.out.println("【发送】..." + msg);
	}
}

package autocloseab;  
 
/** 
 * @author: pyu
 * @Date: 2021/09/09
 */

public class MsgDemo {

	public static void main(String[] args) throws Exception {
		try(NetMessage msg = new NetMessage();)	{
			msg.send("pyu, have a nice day!");
		}
		catch(Exception e) {}
	}

}

编译结果:(仍可关闭资源)

2、 Runtime类

构造方法为private(私有构造方法),属于单例设计模式

      Runtime.maxMemory()

最大可用内存->一般为物理内存的1/4>totalmemory

      Runtime.totalMemory()

可用内存->一般为物理内存的1/64<maxmemory

      Runtime.freeMemory()

空余内存

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值