|
@@ -11,6 +11,7 @@ import java.time.LocalDate;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.util.Optional;
|
|
import java.util.Optional;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
+import java.util.concurrent.locks.ReentrantLock;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @author xucaiqin
|
|
* @author xucaiqin
|
|
@@ -28,6 +29,7 @@ public class OrderUtil {
|
|
|
return 1L;
|
|
return 1L;
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
|
|
+ private static final ReentrantLock lock = new ReentrantLock();
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 流水号
|
|
* 流水号
|
|
@@ -38,9 +40,14 @@ public class OrderUtil {
|
|
|
@SneakyThrows
|
|
@SneakyThrows
|
|
|
public static String get(String prefix) {
|
|
public static String get(String prefix) {
|
|
|
String key = prefix + getYMD();
|
|
String key = prefix + getYMD();
|
|
|
- Long aLong = cache.get(key);
|
|
|
|
|
- Optional.of(aLong).ifPresent(v -> cache.put(key, v + 1));
|
|
|
|
|
- return key + String.format("%05d", aLong);
|
|
|
|
|
|
|
+ lock.lock();
|
|
|
|
|
+ try {
|
|
|
|
|
+ Long aLong = cache.get(key);
|
|
|
|
|
+ Optional.of(aLong).ifPresent(v -> cache.put(key, v + 1));
|
|
|
|
|
+ return key + String.format("%05d", aLong);
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ lock.unlock();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -48,11 +55,4 @@ public class OrderUtil {
|
|
|
return LocalDateTimeUtil.format(LocalDate.now(), DateTimeFormatter.ofPattern(ymd));
|
|
return LocalDateTimeUtil.format(LocalDate.now(), DateTimeFormatter.ofPattern(ymd));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
|
|
- for (int i = 0; i < 10; i++) {
|
|
|
|
|
- System.out.println(get("PRO01"));
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
}
|
|
}
|