我想创建一个lambda数组.问题是lambda可能彼此不同.例:
private interface I0 {
int interface0(int a, int b);
}
private interface I1 {
int interface1(double d);
}
Run Code Online (Sandbox Code Playgroud)
现在,我如何声明一个既可以包含I0又可以包含I1的列表?
List<Object> test = Arrays.asList(
(int a, int b) -> a + b,
(double d) -> d * 2
);
Run Code Online (Sandbox Code Playgroud)
显然Object不起作用.
我正在尝试通过以下命令执行命令列表:
bash -l -c "commands"
Run Code Online (Sandbox Code Playgroud)
但是,当我定义一个变量然后尝试使用它们时,该变量是未定义的(空的)。要清楚:
bash -l -c "var=MyVariable; echo $var"
Run Code Online (Sandbox Code Playgroud) 我在多线程环境中工作。基本上,我有一个unordered_map可以同时被多个线程访问的对象。现在,我的算法是:
function foo(key) {
scoped_lock()
if key exists {
return Map[key]
}
value = get_value()
Map[key] = value
}
Run Code Online (Sandbox Code Playgroud)
显然,这种实现的性能并不好。我可以使用任何算法/方法来提高性能吗?
编辑:
我做了一些测试,我想到了双重检查锁定。所以,我修改了代码:
function foo(key) {
if key exists {
return Map[key]
}
scoped_lock()
if key exists {
return Map[key]
}
value = get_value()
Map[key] = value
}
Run Code Online (Sandbox Code Playgroud)
实际上我只在 scoped_lock() 之前添加了另一个检查。在这种情况下,假设函数被调用了N多次。如果m对foo, where的第一次调用m < N填充了地图,而接下来的N - m调用仅从地图中获取值,则我不需要独占访问权限。此外,在scoped_lock确保线程安全之后还有另一个检查。我对吗?无论如何,第一个代码的执行需要~208s,而第二个代码需要~200s。