我正在学习Java中的多线程,所以现在我正在创建经典的Tortoise和Hare竞赛应用程序.
对于那些不熟悉这个问题的人来说,基本上有两个赛车手:
第一个达到1000米的是赢家.
我的代码现在看起来像这样:
主要课程:
public class THRace {
static Thread hare = new Thread(new ThreadRunner("Hare", 90, 100));
static Thread tortoise = new Thread(new ThreadRunner("Tortoise", 0, 10));
public static void main(String[] args) {
hare.start();
tortoise.start();
}
public static void finished(Thread winner){
if (winner.getName().equals("Hare")) tortoise.interrupt();
else if (winner.getName().equals("Tortoise")) hare.interrupt();
}
}
Run Code Online (Sandbox Code Playgroud)
ThreadRunner类:
import java.util.Random;
public class ThreadRunner implements Runnable {
private String name;
private int rest, speed;
public String getRacerName() {
return name;
}
public ThreadRunner(String name, int rest, …Run Code Online (Sandbox Code Playgroud) 我尝试TryGetValue像往常一样在字典上使用,如下代码:
Response.Context.Skills[MAIN_SKILL].UserDefined.TryGetValue("action", out var actionObj)
Run Code Online (Sandbox Code Playgroud)
我的问题是字典本身可能为 null。我可以简单地使用“?”。在 UserDefined 之前,但随后我收到错误:
"cannot implicitly convert type 'bool?' to 'bool'"
Run Code Online (Sandbox Code Playgroud)
我处理这种情况的最佳方法是什么?UserDefined在使用 TryGetValue 之前是否必须检查是否为 null?因为如果我必须使用Response.Context.Skills[MAIN_SKILL].UserDefined两次,我的代码可能看起来有点混乱:
if (watsonResponse.Context.Skills[MAIN_SKILL].UserDefined != null &&
watsonResponse.Context.Skills[MAIN_SKILL].UserDefined.TryGetValue("action", out var actionObj))
{
var actionName = (string)actionObj;
}
Run Code Online (Sandbox Code Playgroud) 抱歉,如果我不能制作一个更好的标题。我正在尝试在 Haskell 中使用 monad,但遇到了一些麻烦。
所以,我应该做的是:
定义函数
repeat:: IO Bool -> IO () -> IO()
这样就repeat test oper可以重复oper直到条件test满足True
所以,我这样做了:
repeat:: IO Bool -> IO () -> IO()
repeat test oper
= do res <- test
if res
then return ()
else do oper
repeat test oper
Run Code Online (Sandbox Code Playgroud)
但这段代码不起作用。你能解释一下为什么吗?现在我收到“如果输入时解析错误”。我想这只是一个语法错误,但我仍然不知道如何解决这个练习。