File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -284,6 +284,38 @@ Channel.select(
284
284
)
285
285
```
286
286
287
+ #### Time.after()
288
+ Like in Go, you can use ` Time.after(duration) ` to generate a channel that sends a message after the specified time has
289
+ passed. You don't need to close the channel afterward.
290
+
291
+ ``` scala
292
+ import com .github .yruslan .channel ._
293
+
294
+ Channel .select(
295
+ someChannel.recver{v => println(s " Got a message in time: $v" )},
296
+ Time .after(Duration (10 , TimeUnit .SECONDS )).recver{ v => println(" Time is out!" ) }
297
+ )
298
+ ```
299
+
300
+ #### Time.newTicker()
301
+ Like in Go, you can use ` Time.newTicker(duration) ` to generate a channel that sends a message after the specified time.
302
+ When the message is consumed, the ticker will generate another message after the same time duration.
303
+ Tickers should be closed after they are not in use.
304
+
305
+ ``` scala
306
+ import com .github .yruslan .channel ._
307
+
308
+ val ticker = Time .newTicker(Duration (10 , TimeUnit .SECONDS ))
309
+
310
+ Channel .select(
311
+ someChannel.recver{v => println(s " Got a message: $v" )},
312
+ ticker.recver{ _ => println(" Tick." ) }
313
+ )
314
+
315
+ ticker.close()
316
+ ```
317
+
318
+
287
319
### Non-blocking methods
288
320
Go supports non-blocking channel operation by the elegant ` default ` clause in the ` select ` statement. The scala port
289
321
adds separate methods that support non-blocking operations: ` trySend() ` , ` tryRecv() ` and ` trySelect() ` . There is an
You can’t perform that action at this time.
0 commit comments