Skip to content

Instantly share code, notes, and snippets.

@yanglikun
Created August 29, 2017 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yanglikun/6291f74c36990b9f28b6935dcc95242d to your computer and use it in GitHub Desktop.
Save yanglikun/6291f74c36990b9f28b6935dcc95242d to your computer and use it in GitHub Desktop.
Suppliers.memoizeWithExpiration
DateTime dateTime = DateTime.now().plusSeconds(2);
Supplier<String> supplier = Suppliers.memoizeWithExpiration(() -> {
DateTime now = DateTime.now();
if (now.getSecondOfDay() == dateTime.getSecondOfDay()) {
throw new RuntimeException("haha~~" + now.toString("yyyy-MM-hh HH:mm:ss"));
}
return "time:" + now.toString("yyy-MM-dd HH:mm:ss");
}, 1, TimeUnit.SECONDS);
for (int i = 0; i < 10; i++) {
try {
TimeUnit.SECONDS.sleep(1);
System.out.println(supplier.get());
} catch (Exception e) {
System.out.println("异常:"+e.getMessage());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment