假设我们有 10 个线程使用不同的键值调用以下代码。提供给computeIfAbsent方法的“Function”参数是否并行运行,或者computeIfAbsent将“锁定”整个表?
Map<String, String> map = new ConcurrentHashMap<>();
map.computeIfAbsent(key, K -> { // long time operation });
Run Code Online (Sandbox Code Playgroud) 我还是 Elixir 的新手。我正在尝试创建一个接受请求列表并处理每个请求的方法。如果全部通过则返回 {:ok, "success"} 或 {:error, error_reason} 如果失败则返回。
在其他语言中,我可以做这样的事情。假设流程函数返回 {:ok, "success"} 或 {:error, error_reason}。
def func(requests):
for request in requests:
if {:error, error_reason} <- process(request):
return {:error, error_reason}
return {:ok, "success"}
end
Run Code Online (Sandbox Code Playgroud)
在 Elixir 世界中这样做的正确方法是什么?