1212//
1313//===----------------------------------------------------------------------===//
1414
15- import AsyncAlgorithms
1615import Distributed
1716import DistributedActors
1817import Logging
@@ -25,9 +24,6 @@ distributed actor Philosopher: CustomStringConvertible {
2524 private let rightFork : Fork
2625 private var state : State = . thinking
2726
28- private var becomeHungryTimerTask : Task < Void , Error > ?
29- private var finishEatingTimerTask : Task < Void , Error > ?
30-
3127 init ( name: String , leftFork: Fork , rightFork: Fork , actorSystem: ActorSystem ) {
3228 self . actorSystem = actorSystem
3329 self . name = name
@@ -60,12 +56,9 @@ distributed actor Philosopher: CustomStringConvertible {
6056 }
6157
6258 self . state = . thinking
63- self . becomeHungryTimerTask = Task {
64- for await _ in AsyncTimerSequence ( interval: . seconds( 1 ) , clock: ContinuousClock ( ) ) {
65- await self . attemptToTakeForks ( )
66- self . becomeHungryTimerTask? . cancel ( )
67- break
68- }
59+ Task {
60+ try await Task . sleep ( until: . now + . seconds( 1 ) , clock: . continuous)
61+ await self . attemptToTakeForks ( )
6962 }
7063 self . log. info ( " \( self . name) is thinking... " )
7164 }
@@ -151,20 +144,11 @@ distributed actor Philosopher: CustomStringConvertible {
151144 private func becomeEating( ) {
152145 self . state = . eating
153146 self . log. notice ( " \( self . name) began eating! " )
154- self . finishEatingTimerTask = Task {
155- for await _ in AsyncTimerSequence ( interval: . seconds( 3 ) , clock: ContinuousClock ( ) ) {
156- self . stopEating ( )
157- self . finishEatingTimerTask? . cancel ( )
158- break
159- }
147+ Task {
148+ try await Task . sleep ( until: . now + . seconds( 3 ) , clock: . continuous)
149+ self . stopEating ( )
160150 }
161151 }
162-
163- deinit {
164- // FIXME: these are async
165- // self.becomeHungryTimerTask?.cancel()
166- // self.finishEatingTimerTask?.cancel()
167- }
168152}
169153
170154extension Philosopher {
0 commit comments