我有一个关于如何ExecutorService在Java中工作的基本问题.
很难看出简单地创建Threads并行执行某些任务和将每个任务分配给它们之间的区别ThreadPool.
使用起来ExecutorService也非常简单和有效,所以我想知道为什么我们不一直使用它.
这只是一种比另一种方式更快地执行其工作的问题吗?
这里有两个非常简单的例子来说明两种方式之间的区别:
使用执行程序服务:Hello World(任务)
static class HelloTask implements Runnable {
String msg;
public HelloTask(String msg) {
this.msg = msg;
}
public void run() {
long id = Thread.currentThread().getId();
System.out.println(msg + " from thread:" + id);
}
}
Run Code Online (Sandbox Code Playgroud)
使用执行程序服务:Hello World(创建执行程序,提交)
static class HelloTask {
public static void main(String[] args) {
int ntasks = 1000;
ExecutorService exs = Executors.newFixedThreadPool(4);
for (int i=0; i<ntasks; i++) {
HelloTask t = new HelloTask("Hello from …Run Code Online (Sandbox Code Playgroud) 有人可以大致解释一下处理器\xe2\x80\x99s架构与其微架构之间的区别以及它们之间的关系吗?
\n一个应该与其功能部分相关,但另一个我没有看到
\n答案可能很明显,但即使在谷歌搜索后,我仍然可以找到解决这个问题的正确方法.我知道我必须在函数的最后添加一个return语句,以便它可以完成,但是什么样的?
以下程序实现了一种算法,该算法可以在阵列列表中找到第i个最小元素
public int search(ArrayList<Integer> a, int i){
ArrayList<Integer> smaller_than = new ArrayList<Integer>();
ArrayList<Integer> greater_than = new ArrayList<Integer>();
int pivot = a.get(i);
for(int j = 0; j < a.size(); j++) {
if (a.get(j) <= pivot){
smaller_than.add(a.get(j));}
if (a.get(j) > pivot){
greater_than.add(a.get(j));}}
if (smaller_than.size() == i)
return smaller_than.get(i);
else if (smaller_than.size() < i)
return search(greater_than, i-smaller_than.size());
else if (smaller_than.size() > i)
return search(smaller_than, i);
}
Run Code Online (Sandbox Code Playgroud)
我显然无法在最后添加return null,所以你能帮我找到解决方案吗?
以下程序应检查输入n是否可被n中的数字之和整除
module Harshad where
isHarshad :: Int -> Bool
isHarshad n = (mod n (sumDig n)) == 0)
where
sumDig n
| (floor (n / 10) == 0) = n -- n consists of only one digit
| otherwise = sumDig (floor (n / 10)) + floor (10*(n - floor (n / 10)))
Run Code Online (Sandbox Code Playgroud)
我得到以下编译错误:*由于使用`sumDig'而导致(RealFrac Int)没有实例
即使在尝试添加各种转换后,我仍然卡住了.