当然,我可以对数组CallableTask
进行排序,消除重复import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class ExecutorServiceFutureCallableExample {
public static void main(String[] args) throws InterruptedException, ExecutionException {
List<Future<String>> futuresList = new ArrayList<>();
ExecutorService executorService = Executors.newCachedThreadPool();
ExecutorServiceFutureCallableExample.CallableTask callableTask1 = new ExecutorServiceFutureCallableExample.CallableTask(2000);
ExecutorServiceFutureCallableExample.CallableTask callableTask2 = new ExecutorServiceFutureCallableExample.CallableTask(1000);
ExecutorServiceFutureCallableExample.CallableTask callableTask3 = new ExecutorServiceFutureCallableExample.CallableTask(3000);
System.out.println("### Starting submitting tasks");
// submit the callable and register the returned future object so that it can be processed later.
futuresList.add(executorService.submit(callableTask1));
futuresList.add(executorService.submit(callableTask2));
futuresList.add(executorService.submit(callableTask3));
System.out.println("### Finished submitting tasks");
for (int i = 0; i < futuresList.size(); i++) {
// here "get()" waits for the future tasks to be returned.
System.out.println(futuresList.get(i).get());
}
System.out.println("### Finished.");
}
static class CallableTask implements Callable<String>{
private long timeToSleep;
CallableTask(long _timeToSleep){
this.timeToSleep = _timeToSleep;
}
@Override
public String call() throws Exception {
String str = new Date() + ": Processing - " + this.hashCode() + " | " + Thread.currentThread() + ", slept for seconds - " + timeToSleep;
System.out.println(str);
Thread.sleep(timeToSleep);
return str + " ||||| completed at: " + new Date();
}
public long getTimeToSleep() {
return timeToSleep;
}
public void setTimeToSleep(long timeToSleep) {
this.timeToSleep = timeToSleep;
}
}
}
并消除空白sort()
。我可以在三行中完成,然后为我必须处理的十个数组重复这三行。
但我希望它至少有点优雅,所以我尝试结合这三个操作。它确实适用于前两个,然后我将它推得太远并应用了sort()
array_unique()
这产生了:
严格标准:只应通过引用传递变量
那么实现这个数组处理目标最优雅的方法是什么?
帮助我理解失败原因的好处。
答案 0 :(得分:1)
作为概念的证明,您可以避免创建中间变量并改变原始数组。从SplMinHeap
查看Standard PHP Library (SPL)。您可以将此类用于不可变排序:
h.keys().map {|x| x.split(" ")}.each do |e|
puts "#{e.first.split(":").last} #{e.last.split(":").last}"
end
# xy@xy.com azerty
# yy@yy.com qwerty
这是working demo。
答案 1 :(得分:1)
由于滥用sort()
功能,您的代码$testArray = sort(array_filter(array_unique($testArray)));
无法正常工作:
sort()
返回的值为TRUE
或FALSE
。不是你期望的数组。sort()
元素将排列在数组本身上。所以它需要一个实际的数组来处理它;不是其他函数的重新定义的值,它实际上不作为变量存在。这就是错误的原因
只应通过引用传递变量...
了解这一点,并牢记在PHP the value of an assignment expression is the value assigned. That is, the value of "$a = 3" is 3中。修复错误的第一种方法可能是:
sort($testArray = array_filter(array_unique($testArray)));
......但它也不起作用。赋值返回$ testArray的值,而不是$ testArray本身。和以前一样的问题。
此时,解决问题的最简单方法是没有不必要的开销:使用两行代码而不是一行。
$testArray = array_filter(array_unique($testArray));
sort($testArray);
测试here。