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. hdu 3405 删掉某点后 求最小生成树

    给出N个点的坐标 边的权值为两点间的距离 删掉其中某点 求最小生成树的权值和 要求这权值最小 因为最多50个点 所以具体是删哪个点 用枚举假如有4个点 就要求4次最小生成树 分别是2 3 4 | 1 ...

  2. hihocoder 编程练习赛23

    第一题:H国的身份证号码I 题意:一个N位的正整数(首位不能是0).每位数字都小于等于K,并且任意相邻两位数字的乘积也小于等于K.按从小到大的顺序输出所有合法的N位号码,每个号码占一行. 思路:dfs ...

  3. bzoj4184

    题解: 按时间分治线段树 然后线性基维护一下就好了 尝试了一下循环展开并没有什么效果 代码: #include <bits/stdc++.h> using namespace std; ; ...

  4. HDU Tody HDU2112

    不想用floyd了 也不一定适合  floyd只能处理小数据 dijkstra算法 wa了很久   一个是dijkstra里面的u   导致RE了无数次   下标溢出 还有就是注意细节  当起点和终点 ...

  5. 016 SpringMVC中重定向

    1.介绍 2.index <%@ page language="java" contentType="text/html; charset=utf-8" ...

  6. 045 介绍UDF,以及完成大小写的转换

    一:概述 1.UDF 用户自定义函数,用java实现自定义的需求 User Defined Function-----UDF. 2.UDF的类型 udf:一进一出 udaf:多进一出 udtf:一进多 ...

  7. hdu 1272 小希的迷宫【并查集】

    <题目链接> 小希的迷宫 Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的 ...

  8. kali linux系统配置 避免踩坑

    使用kali一个多月,重新安装kail也三次了,下面是安装完系统后,做如下系统配置,必须要做的配置,让自己以后不要踩坑. kail更新系统 1.添加更新源,前面已经介绍,不再多说 2获取更新数据,命令 ...

  9. x,y

    x,y在二维里,横纵坐标容易反,有关处理要小心.

  10. VB.NET 编程元素支持更改总结

    Visual Basic 2005 更改了它支持各种编程元素的方式,主要是为了提供与公共语言运行库的互操作性.许多 Visual Basic 6.0 元素被重新命名,重新分类或与 Visual Bas ...