Akka2使用探索3(Duration 和 Deadline)
2019獨角獸企業重金招聘Python工程師標準>>>
akka提供了兩個關于時長的數據類型:Duration 和 Deadline,比如5秒鐘這種含義。
Duration.Inf表示無限,Duration.MinusInf表示負無限
Deadline, 表示一個絕對的時間點,意義是最終期限,并且支持通過計算當前時間到deadline之間的差距來生成Duration
下面是使用方法:
?
import akka.util.Duration
import java.util.concurrent.TimeUnit
import akka.util.FiniteDuration
import akka.util.Deadline
import akka.dispatch.Await
import akka.dispatch.Futures
import java.util.concurrent.Callable
import akka.dispatch.Future
import akka.actor.ActorSystem
class DurationTest extends GroovyTestCase {
??? def testDurationUse() {
??????? println new FiniteDuration(5, TimeUnit.SECONDS)
??????? println Duration.create(5d, TimeUnit.SECONDS)
??????? println Duration.create(5, "second")
??????? println Duration.create(5L, "second")
??????? println()
??????? println Duration.parse("5second")
??????? println Duration.parse("5 second")
??????? println Duration.parse("5 seconds")
??????? println Duration.parse("5seconds")
??????? println()
??????? println Duration.Inf()
??????? println Duration.Zero()
??????? println Duration.MinusInf()
??? }
??? def testDeadline() {
??????? Deadline d = Duration.create(5, "second").fromNow()
??????? def future = Futures.future(new Callable<String>() {
??????????? String call() {
??????????????? println "start...";
??????????????? sleep(1000 * 60);
??????????????? println("end...");
??????????????? return "sd";
??????????? }
??????? }, ActorSystem.create("test").dispatcher())
??????? String result = (String) Await.result(Futures.future({println "start..."; sleep(1000 * 60); println("end..."); return "sd";} as Callable, ActorSystem.create("test").dispatcher()), d.timeLeft())
??????? sleep(1000 * 60)
??? }
??? def testDeadline2() {
??????? Future<String> f = Futures.future(new Callable<String>() {
??????????? public String call() {
??????????????? return "Hello" + "World";
??????????? }
??????? }, ActorSystem.create("test").dispatcher());
??????? String result = (String) Await.result(f, Duration.create(5, "second"));
??????? println result
??? }
}
轉載于:https://my.oschina.net/xiefeifeihu/blog/81112
總結
以上是生活随笔為你收集整理的Akka2使用探索3(Duration 和 Deadline)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: struts2对action的指定方法进
- 下一篇: fedora17的gnome3桌面美化