Created
August 29, 2017 01:58
-
-
Save yanglikun/6291f74c36990b9f28b6935dcc95242d to your computer and use it in GitHub Desktop.
Suppliers.memoizeWithExpiration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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