class_name & class_name :: operator= ( const class_name & ) (2)
Run Code Online (Sandbox Code Playgroud)
(2)当不能使用复制和交换习语时,复制赋值运算符的典型声明
我们何时应该避免使用复制和交换习惯用法?
什么时候"完全不能使用"?
是否有现实生活中的情况,即复制和交换以及零规则都不适用?
我确实找到了这个问题,但它过于具体,没有包含任何关于如何识别此类案例的指南.
可能听起来有点傻,但我不精通 Java,所以想确保:
如果有两个代码点
一世:
if (_myVar == null)
{
return;
}
Run Code Online (Sandbox Code Playgroud)
二:
synchronized (_myLock)
{
_myVar = MyVarFactory.create(/* real params */)
}
Run Code Online (Sandbox Code Playgroud)
编辑:假设这_myVar是一个复杂的对象(即不是布尔值、整数或长整数)而是一个完全成熟的 java 类,它具有一些父类等。
假设 I 和 II 可以同时在不同的线程上运行,我认为在 C++ 中这将是“数据竞争”,但是我不确定 Java 中的情况。
给定一些任意的上下文(例如 Junit 单元测试,而不是特定的,不一定是“主”线程)。
像这样的代码必须引入至少 2 个线程吗?
public static void main (String[] args)
{
CompletableFuture<Void> s = new CompletableFuture<>();
CompletableFuture<Void> f = new CompletableFuture<>();
CompletableFuture<Void> someContext = CompletableFuture.supplyAsync(() ->
{
try{
System.out.println(Thread.currentThread().getId());
CompletableFuture<String> update =
CompletableFuture.supplyAsync(
() -> {
String ans = null;
try {
System.out.println(Thread.currentThread().getId());
ans = "Hello";
} catch (Exception e) {
ans = e.toString();
} finally {
s.complete(null);
return ans;
}
});
s.get();
System.out.println(s.isDone());
} catch (Exception e) {
System.out.println("Some error");
return null;
}
return null;
});
System.out.println(f.isDone()); …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用nan来计算附加组件中浮点数组上的某些内容,然后将其作为Float32Array.
但是,虽然 args 具有IsNumber()和NumberValue()功能,但它只有一个IsFloat32Array()功能而没有Float32Array()。
NAN_METHOD(Calc) {
NanScope();
if (args.Length() < 2) {
NanThrowTypeError("Wrong number of arguments");
NanReturnUndefined();
}
if (!args[0]->IsNumber() || !args[1]->IsFloat32Array()) {
NanThrowTypeError("Wrong arguments");
NanReturnUndefined();
}
/* a vector of floats ? */ args[0]-> ???;
double arg1 = args[1]->NumberValue();
// some calculation on the vector
NanReturnValue(/* Return as a Float32Array array */);
}
Run Code Online (Sandbox Code Playgroud) 我想打电话给cancel上CompletableFuture。
从文档看来:
如果尚未完成,则使用 CancellationException 完成此 CompletableFuture。尚未完成的从属 CompletableFutures 也将异常完成,此 CancellationException 会导致 CompletionException。
它应该异常地完成它们,这正是我所期望的,但相反,它会立即抛出 CancellationException。
这是一个示例代码
CompletableFuture<?> f = CompletableFuture.supplyAsync(() -> false);
f.cancel(true); // Line 7.
f.join();
Run Code Online (Sandbox Code Playgroud)
使用重现:https : //www.mycompiler.io/view/2v1ME4u
Exception in thread "main" java.util.concurrent.CancellationException
at java.base/java.util.concurrent.CompletableFuture.cancel(CompletableFuture.java:2396)
at Main.main(Main.java:7)
Run Code Online (Sandbox Code Playgroud)
7号线是f.cancel(true);线。
java concurrency java.util.concurrent java-8 completable-future