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的更多相关文章

  1. 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 ...

  2. 《Effective Java》读书笔记 - 9.异常

    Chapter 9 Exceptions Item 57: Use exceptions only for exceptional conditions 这条item的意思就是,千万不要用except ...

  3. Effective Java 目录

    <Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...

  4. 【Effective Java】阅读

    Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...

  5. Effective Java通俗理解(下)

    Effective Java通俗理解(上) 第31条:用实例域代替序数 枚举类型有一个ordinal方法,它范围该常量的序数从0开始,不建议使用这个方法,因为这不能很好地对枚举进行维护,正确应该是利用 ...

  6. Effective Java 第三版——49. 检查参数有效性

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  7. Effective Java

    Effective Java 创建和销毁对象---考虑用静态工厂方法代替构造器 构造器是创建一个对象实例最基本也最通用的方法,大部分开发者在使用某个class的时候,首先需要考虑的就是如何构造和初始化 ...

  8. Effective Java通俗理解(持续更新)

    这篇博客是Java经典书籍<Effective Java(第二版)>的读书笔记,此书共有78条关于编写高质量Java代码的建议,我会试着逐一对其进行更为通俗易懂地讲解,故此篇博客的更新大约 ...

  9. Effective Java 第三版——1. 考虑使用静态工厂方法替代构造方法

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

随机推荐

  1. 服务器唯一id生成规则

    在使用hashCode的时候,发现会出现相同id,虽然几率很小.虽然发现并不是hashCode的原因,而是其他逻辑的问题. 但是还是试着自己写了一个id生成器,有些id是int的,比如说任务id:有些 ...

  2. Eclipse魔法堂:任务管理器

    一.前言        Eclipse的任务管理器为我们提供一个方便的入口查看工程代办事宜,并定位到对应的代码行继续之前的工作. 二.使用示例        示例1: /** * @Descripti ...

  3. LeetCode - 31. Next Permutation

    31. Next Permutation Problem's Link ---------------------------------------------------------------- ...

  4. 2014 Asia AnShan Regional Contest --- HDU 5073 Galaxy

    Galaxy Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=5073 Mean: 在一条数轴上,有n颗卫星,现在你可以改变k颗 ...

  5. 【EF 译文系列】重试执行策略的局限性(EF 版本至少为 6)

    原文链接:Limitations with Retrying Execution Strategies (EF6 onwards) 当使用重试执行策略的时候,大体有以下两种局限性: 不支持以流的方式进 ...

  6. ToDoList:一款非常优秀的任务管理软件 —— 工具类

    ToDoList是一款非常优秀的任务管理软件,用户可以方便地组织和安排计划.这是一个开源的项目,很多细节都考虑到了,推荐大家使用~ ToDoList 帮你把要做的事情列出来,一项一项,类似思维导图. ...

  7. LGLSearchBar

    平时我们都是用UITextFeild 来写搜索框, 最近有时间就自己重新封装了UISearchBar, 他可以自行修改里面的属性来达到我们使用的要求. 源代码下载地址:https://github.c ...

  8. online judge(ACM) 的设计与分析 (有c#demo)

    ACM.OJ,算法在线判题系统. 帮朋友完成毕业设计而写的,软件环境windows系统,语言是C# winform(因为我不熟悉asp.net,现在暂用winform写的demo). 看了下其他相关论 ...

  9. CSS选择器特殊性与重要性

    特殊性 在编写CSS代码的时候,我们会出现多个样式规则作用于同一个元素的情况,例如 <!-- HTML --> <header> <nav class="nav ...

  10. ABAP中Conversion Routine示例

          在SAP的Domain定义中,Output Length下面有个Convers. routine的标识,这是SAP用来进行输入输出转换的.我们知道,屏幕上的I/O字段都是字符串形式的,而数 ...