1
zyy
20 hours ago 4fefff17528a878d345ff3311c297a66a671b8d6
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
package com.yami.trading.huobi.websocket.utils;
 
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
 
public class IdGenerator {
  private static AtomicInteger COUNTER = new AtomicInteger();
 
  private static Set<Long> TIME_SET = new HashSet<>();
 
  public static Long getNextId(){
 
    Long time = System.currentTimeMillis();
 
    if (!TIME_SET.contains(time)) {
      COUNTER.set(0);
      TIME_SET.clear();
      TIME_SET.add(time);
    }
 
    Long id = (time * 1000) + COUNTER.addAndGet(1);
    return id;
  }
}