import java.util.concurrent.atomic.AtomicInteger;
public class AtomicPositiveInteger extends Number { private static final long serialVersionUID = -3038533876489105940L; private final AtomicInteger i; public AtomicPositiveInteger() {
i = new AtomicInteger();
} public AtomicPositiveInteger(int initialValue) {
i = new AtomicInteger(initialValue);
} public final int getAndIncrement() {
for (;;) {
int current = i.get();
int next = (current >= Integer.MAX_VALUE ? 0 : current + 1);
if (i.compareAndSet(current, next)) {
return current;
}
}
} public final int getAndDecrement() {
for (;;) {
int current = i.get();
int next = (current <= 0 ? Integer.MAX_VALUE : current - 1);
if (i.compareAndSet(current, next)) {
return current;
}
}
} public final int incrementAndGet() {
for (;;) {
int current = i.get();
int next = (current >= Integer.MAX_VALUE ? 0 : current + 1);
if (i.compareAndSet(current, next)) {
return next;
}
}
} public final int decrementAndGet() {
for (;;) {
int current = i.get();
int next = (current <= 0 ? Integer.MAX_VALUE : current - 1);
if (i.compareAndSet(current, next)) {
return next;
}
}
} public final int get() {
return i.get();
} public final void set(int newValue) {
if (newValue < 0) {
throw new IllegalArgumentException("new value " + newValue + " < 0");
}
i.set(newValue);
} public final int getAndSet(int newValue) {
if (newValue < 0) {
throw new IllegalArgumentException("new value " + newValue + " < 0");
}
return i.getAndSet(newValue);
} public final int getAndAdd(int delta) {
if (delta < 0) {
throw new IllegalArgumentException("delta " + delta + " < 0");
}
for (;;) {
int current = i.get();
int next = (current >= Integer.MAX_VALUE - delta + 1 ? delta - 1 : current + delta);
if (i.compareAndSet(current, next)) {
return current;
}
}
} public final int addAndGet(int delta) {
if (delta < 0) {
throw new IllegalArgumentException("delta " + delta + " < 0");
}
for (;;) {
int current = i.get();
int next = (current >= Integer.MAX_VALUE - delta + 1 ? delta - 1 : current + delta);
if (i.compareAndSet(current, next)) {
return next;
}
}
} public final boolean compareAndSet(int expect, int update) {
if (update < 0) {
throw new IllegalArgumentException("update value " + update + " < 0");
}
return i.compareAndSet(expect, update);
} public final boolean weakCompareAndSet(int expect, int update) {
if (update < 0) {
throw new IllegalArgumentException("update value " + update + " < 0");
}
return i.weakCompareAndSet(expect, update);
} public byte byteValue() {
return i.byteValue();
} public short shortValue() {
return i.shortValue();
} public int intValue() {
return i.intValue();
} public long longValue() {
return i.longValue();
} public float floatValue() {
return i.floatValue();
} public double doubleValue() {
return i.doubleValue();
} public String toString() {
return i.toString();
} @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((i == null) ? 0 : i.hashCode());
return result;
} @Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
AtomicPositiveInteger other = (AtomicPositiveInteger) obj;
if (i == null) {
if (other.i != null) return false;
} else if (!i.equals(other.i)) return false;
return true;
} }

实现一个原子的正整数类:AtomicPositiveInteger的更多相关文章

  1. 不用synchronized块的话如何实现一个原子的i++?

    上周被问到这个问题,没想出来,后来提示说concurrent包里的原子类.回来学习一下. 一.何谓Atomic? Atomic一词跟原子有点关系,后者曾被人认为是最小物质的单位.计算机中的Atomic ...

  2. VC++ 一个简单的Log类

    在软件开发中,为程序建立Log日志是很必要的,它可以记录程序运行的状态以及出错信息,方便维护和调试. 下面实现了一个简单的Log类,使用非常简单,仅供参考. // CLogHelper.h : hea ...

  3. Android 分享一个SharedPreferences的工具类,方便保存数据

    我们平常保存一些数据,都会用到SharedPreferences,他是保存在手机里面的,具体路径是data/data/你的包名/shared_prefs/保存的文件名.xml, SharedPrefe ...

  4. Hibernate的多表查询,分装到一个新的实体类中的一个方法

    不知道是否还有其他方法实现,请高人指点. 如果涉及到多张表多字段查询,并且想利用查询出来的字段在界面层构建一个新的实体类,可以使用这种方法: 如果查询出来的多字段中,有多个字段的名字都相同(如想查询出 ...

  5. 课堂练习:给定一个十进制的正整数,写下从1开始,到N的所有整数,然后数一下其中出现“1”的个数。

    题目 1 给定一个十进制的正整数,写下从1开始,到N的所有整数,然后数一下其中出现“1”的个数. 2 要求: (1) 写一个函数 f(N) ,返回1 到 N 之间出现的“1”的个数.例如 f(12)  ...

  6. 一个通用数据库访问类(C#,SqlClient)

    本文转自:http://www.7139.com/jsxy/cxsj/c/200607/114291.html使用ADO.NET时,每次数据库操作都要设置connection属性.建立connecti ...

  7. 编辑一个小的smarty类

    首先先建立两个文件夹,一个temp,存储编译前的文件,一个comp,存储编译后的文件,编译前的文件使用{$title}代替<?php echo $title; ?>,然后将前者编译成后者再 ...

  8. C++实现一个单例模板类

    单例模式在项目开发中使用得比较多,一个单例的模板类显得很有必要,避免每次都要重复定义一个单例类型 //非多线程模式下的一个单例模板类的实现 // template_singleton.h #inclu ...

  9. 一个基础的CURL类

    /** * 一个基础的CURL类 * * @author Smala */ class curl{ public $ch; public $cookie = '/cookie'; public $rs ...

随机推荐

  1. kafka删除topic数据

    一.概述 生产环境中,有一个topic的数据量非常大.这些数据不是非常重要,需要定期清理. 要求:默认保持24小时,某些topic 需要保留2小时或者6小时 二.清除方式 主要有3个: 1. 基于时间 ...

  2. webservice之restlet实现

    转自LifeBa,http://www.lifeba.org/arch/restlet_develop_application_component_2.html但有改动,主要改动有:1. 修改了web ...

  3. [转] 理解Web路由

    1. 什么是路由 在Web开发过程中,经常会遇到『路由』的概念.那么,到底什么是路由?简单来说,路由就是URL到函数的映射. 2. router和route的区别 route就是一条路由,它将一个UR ...

  4. Mysql 账号过期问题

    1.ALTER USER 'root'@'localhost' PASSWORD EXPIRE; 一旦某个用户的这个选项设置为”Y”,那么这个用户还是可以登陆到MySQL服务器,但是在用户未设置新密码 ...

  5. Enrolment API

    由于Moodle 2.0有一个用户注册的新概念,它们完全独立于角色和功能.能力通常与注册状态结合使用. 什么是注册? 登记的用户可以完全参加一门课程.活跃用户注册允许用户输入课程.只有注册的用户可能是 ...

  6. 浅谈html5 video 移动端填坑记

    这篇文章主要介绍了浅谈html5 video 移动端填坑记,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧 本文介绍了html5 video 移动端填坑记,分享给大家,具体 ...

  7. POJ 2337 Catenyms(有向欧拉图:输出欧拉路径)

    题目链接>>>>>> 题目大意: 给出一些字符串,问能否将这些字符串  按照 词语接龙,首尾相接  的规则 使得每个字符串出现一次 如果可以 按字典序输出这个字符串 ...

  8. C#读取大数据量Excel

    var worksheet = workbook.Worksheets["工作表1"]; var maxN = worksheet.Range["A1"].En ...

  9. [ 原创 ] Centos7.6安装Mysql5.7

    https://blog.csdn.net/shj_php/article/details/86712408 CentOS7下安装MySQL5.7安装与配置(YUM) http://blog.csdn ...

  10. Alpha测试

    1.测试计划 测试工作安排 成员名称 成员工作安排 林凯 注册登录页面相关功能测试 刘华强 主页面相关功能测试 吴文清 管理员页面相关功能测试 谢孟轩 用户页面相关功能测试 蔡振翼 回归测试 测试工具 ...