不好意思想请教一个作业问题,对大神来说应该不难,但我在一个地方卡住了。需要实现一个用户输入指令,然后运用多执行绪处理背后的运算,回传用户讯息。运算逻辑的部分老师已经写好,不用管他怎么计算的,用户也不用真的输入东西,用 System.out.println的方式呈现就好, 比如说System.out.println(solution.runCommand("start 10456060"));我需要印出Started 10456060,程式码以及打印结果连结如下,Solution那个类是新加的,功能有实现出来。
https://scalloped-ceres-c7b.notion.site/Thread-a7f5586100724fe2b18ef8d0b6e707ad
我的问题是
private void startCalculation(long n) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
SlowCalculator s = new SlowCalculator(n);
try {
// s.run();
results.put(n, s.getResult());
} finally {
runningCalculations.remove(n);
}
}
});
runningCalculations.put(n, t);
t.start();
}
这个方法,又另外new了一个Runnable(),他才能跑,但在SlowCalculator类里已经引用Runnable应该不需要再这样写才对,但又不会修改,因此上来询问,感激不尽。