Effective Java 64 Strive for failure atomicity
Principle
Failure atomic - A failed method invocation should leave the object in the state that it was in prior to the invocation.
Ways to achieve failure atomic
1. Method operates on Immutable objects
It's free to failure atomic since the state of the immutable objects is consistent when it's created and can't be modified thereafter.
2. Method operates on mutable objects
- Check parameters for validity before performing operation(Item 38).
public Object pop() {
if (size == 0)
throw new EmptyStackException();
/* If there is no such checking there will be non-abstract exception thrown by the app and the size field will be in an invalid state after the exception. */
Object result = elements[--size];
elements[size] = null; // Eliminate obsolete reference
return result;
}
- Order the computation
Any part that may fail takes place before any part that modifies the object.
3. Write recovery code that intercepts a failure that occurs in the midst of an operation and causes the object to roll back its state to the point before the operation began.
This approach is used mainly for durable (disk-based) data structures.
4. Perform the operation on a temporary copy of the object and to replace the contents of the object with the temporary copy once the operation is complete.
Collections.sort dumps its input list into an array prior to sorting to reduce the cost of accessing elements in the inner loop of the sort. This is done for performance, but as an added benefit, it ensures that the input list will be untouched if the sort fails.
Summary
Any generated exception that is part of a method's specification should leave the object in the same state it was in prior to the method invocation. Where this rule is violated, the API documentation should clearly indicate what state the object will be left in.
Effective Java 64 Strive for failure atomicity的更多相关文章
- Effective Java Index
Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...
- 《Effective Java》读书笔记 - 9.异常
Chapter 9 Exceptions Item 57: Use exceptions only for exceptional conditions 这条item的意思就是,千万不要用except ...
- Effective Java 目录
<Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...
- 【Effective Java】阅读
Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...
- Effective Java通俗理解(下)
Effective Java通俗理解(上) 第31条:用实例域代替序数 枚举类型有一个ordinal方法,它范围该常量的序数从0开始,不建议使用这个方法,因为这不能很好地对枚举进行维护,正确应该是利用 ...
- Effective Java 第三版——49. 检查参数有效性
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- Effective Java
Effective Java 创建和销毁对象---考虑用静态工厂方法代替构造器 构造器是创建一个对象实例最基本也最通用的方法,大部分开发者在使用某个class的时候,首先需要考虑的就是如何构造和初始化 ...
- Effective Java通俗理解(持续更新)
这篇博客是Java经典书籍<Effective Java(第二版)>的读书笔记,此书共有78条关于编写高质量Java代码的建议,我会试着逐一对其进行更为通俗易懂地讲解,故此篇博客的更新大约 ...
- Effective Java 第三版——1. 考虑使用静态工厂方法替代构造方法
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
随机推荐
- [转]提高 Linux 上 socket 性能,加速网络应用程序的 4 种方法
原文链接:http://www.ibm.com/developerworks/cn/linux/l-hisock.html 使用 Sockets API,我们可以开发客户机和服务器应用程序,它们可以在 ...
- WebApi传参总动员(二)
上篇,从最简单的string入手.本篇演示了从请求的输入流中获取实体.api: public class ValuesController : ApiController { [HttpPost] p ...
- 客户端(Winform窗体)上传文件到服务器(web窗体)简单例子
客户端:先创建一个winform窗体的应用程序项目 项目结构
- PHPWind 8.7中代码结构与程序执行顺序
pw9在此不谈,他是完全重构的作品,是完全MVC下的体系.当然,其中很多东西在PW8.7下已经可见端倪. 主要代码结构 1. 以现代的观点,PW是多入口应用模式,程序根目录下的文件几乎都是入口: 2. ...
- 线段树---Atlantis
题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110064#problem/A Description There are se ...
- 状态压缩DP---Hie with the Pie
http: //acm.hust.edu.cn/vjudge/contest/view.action?cid=110044#problem/B Description The Pizazz Pizze ...
- php表单中如何获取单选按钮与复选按钮的值
php代码中获取表单中单选按钮的值:(单选按钮只能让我们选择一个,这里有一个"checked"属性,这是用来默认选取的,我们每次刷新我们的页面时就默认为这个值.) 例:<fo ...
- [python学习笔记]Day2
摘要: 对象 对于python来说,一切事物都是对象,对象基于类创建: 注:查看对象相关成员 var,type,dir 基本数据类型和序列 int内部功能 class int(object): def ...
- MaterialRefreshLayout
以上就介绍了比SwipeRefreshLayout更漂亮和强大的下拉刷新控件:Android-MaterialRefreshLayout 1.xml <?xml version="1. ...
- FPSCalc——简单FPS观测类
利用Unity做的手游项目很多时候要保证流畅度,流畅度最直观的表现就是帧率FPS.Unity编辑器模式下的帧率观测几乎没有意义,所以还是自己实现的好. 这里给一个前人写的类,我几乎原封不动,该类只有一 ...