问题描述
在我现有的 Scala 代码中,我将 #include<stdio.h>
#include<time.h> //needs to be included to use time()
int main(){
time_t time1;
time_t time3;
long int f=0;
time(&time1);
int b;
while(f<=6){ //'6' is the number of seconds the program will ask for input for
scanf("%d",&b);
time(&time3);
f=time3-time1;
}
printf("Time's up!");
}
替换为 Thread.sleep(10000)
,因为它不会阻塞线程池中的线程(性能问题)。当程序运行时,它不会在这一行等待(当然在第一种情况下它会)。我需要为 ZIO 方法添加任何额外的代码才能工作吗?
ZIO.sleep(Duration.fromScala(10.seconds))
解决方法
如您所说,ZIO.sleep
只会挂起正在运行的纤程,而不会挂起操作系统线程。
如果你想睡觉后开始做某事,你应该在睡觉后把它链接起来:
// value 42 will only be computed after waiting for 10s
val io = ZIO.sleep(Duration.fromScala(10.seconds)).map(_ => 42)