[英]Java synchronized block: Lock block until object instantiated
我想同步我的代码块并仅在实例化 object 连接时释放它,我该怎么做?
public void myMethod() {
Connection connection = null;
synchronized (connection) {
// Set configuration, block of code to synchronize
}
connection = getConnection(configuration); // Should be released after this
}
使用一个单独的东西作为你的显示器:
private final Object myMethodLock = new Object();
public void myMethod() {
Connection connection = null;
synchronized (myMethodLock) {
connection = getConnection(configuration);
}
// myMethodLock has been released.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.