自动化立体仓库 - WCS系统
#
luxiaotao1123
2020-08-05 3e3959bf84c673b3cb366e6225a6b5c8dfbea903
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.zy.common.HslCommunication.BasicFramework;
 
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
/**
 * 一个简单的不持久化的序号自增类,采用线程安全实现,并允许指定最大数字,到达后清空从指定数开始
 */
public class SoftIncrementCount {
 
 
    /**
     * 实例化一个自增信息的对象,包括最大值
     * @param max 数据的最大值,必须指定
     * @param start 数据的起始值,默认为0
     */
    public SoftIncrementCount( long max, long start )
    {
        this.start = start;
        this.max = max;
        current = start;
        hybirdLock = new ReentrantLock();
    }
 
 
    private long start = 0;
    private long current = 0;
    private long max = Long.MAX_VALUE;
    private Lock hybirdLock;
 
 
    /**
     * 获取自增信息
     * @return 值
     */
    public long GetCurrentValue( )
    {
        long value = 0;
        hybirdLock.lock( );
 
        value = current;
        current++;
        if (current > max)
        {
            current = 0;
        }
 
        hybirdLock.unlock( );
        return value;
    }
 
}