lambda表达式中使用的变量应该是final或者有效的final
当我尝试使用calTz它时显示此错误.
private TimeZone extractCalendarTimeZoneComponent(Calendar cal, TimeZone calTz) {
try {
cal.getComponents().getComponents("VTIMEZONE").forEach(component -> {
VTimeZone v = (VTimeZone) component;
v.getTimeZoneId();
if (calTz == null) {
calTz = TimeZone.getTimeZone(v.getTimeZoneId().getValue());
}
});
} catch (Exception e) {
log.warn("Unable to determine ical timezone", e);
}
return null;
}
Run Code Online (Sandbox Code Playgroud) 每次在非最终类字段上同步时都会显示警告.这是代码:
public class X
{
private Object o;
public void setO(Object o)
{
this.o = o;
}
public void x()
{
synchronized (o) // synchronization on a non-final field
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以我用以下方式改变了编码
public class X
{
private final Object o;
public X()
{
o = new Object();
}
public void x()
{
synchronized (o)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不确定上面的代码是在非final类字段上同步的正确方法.如何同步非最终字段?
假设我在Spring容器中定义了一个bean(例如BeanA),并将此bean注入到一个对象中.(例如BeanAUser)
在运行期间,我可以使用另一个bean实例来替换弹簧容器内的原始BeanA吗?并且还将这个新的bean实例重新注入BeanAUser以替换原来的BeanA?
我正在使用一个在发送请求时需要回调的框架.每个回调都必须实现此接口.回调中的方法是异步调用的.
public interface ClientCallback<RESP extends Response>
{
public void onSuccessResponse(RESP resp);
public void onFailureResponse(FailureResponse failure);
public void onError(Throwable e);
}
Run Code Online (Sandbox Code Playgroud)
要使用TestNG编写集成测试,我想要一个阻塞回调.所以我使用CountDownLatch来在线程之间进行同步.
这里真的需要AtomicReference还是原始引用好吗?我知道如果我使用原始引用和原始整数(而不是CountDownLatch),代码将无法工作,因为无法保证可见性.但由于CountDownLatch已经同步,我不确定是否需要AtomicReference的额外同步.注意:Result类是不可变的.
public class BlockingCallback<RESP extends Response> implements ClientCallback<RESP>
{
private final AtomicReference<Result<RESP>> _result = new AtomicReference<Result<RESP>>();
private final CountDownLatch _latch = new CountDownLatch(1);
public void onSuccessResponse(RESP resp)
{
_result.set(new Result<RESP>(resp, null, null));
_latch.countDown();
}
public void onFailureResponse(FailureResponse failure)
{
_result.set(new Result<RESP>(null, failure, null));
_latch.countDown();
}
public void onError(Throwable e)
{
_result.set(new Result<RESP>(null, null, e));
_latch.countDown();
}
public Result<RESP> …Run Code Online (Sandbox Code Playgroud) 我经常发现自己使用这个习语:
AtomicReference<MyCoolObject> coolReference = new AtomicReference(new MyCoolObject());
getOptionalValue().ifPresent(presentValue -> {
coolReference.set(new MyCoolObject(presentValue));
});
return coolReference.get();
Run Code Online (Sandbox Code Playgroud)
这是一个坏习惯,还是一个合法的使用AtomicReference?
我已经从类A创建了三个对象.所有这三个对象都可以更新存储在类A中的私有静态volatile变量中的值.更新此变量是在具有特定条件的同步块中完成的.我想通过使用锁对象来同步块.
首先,对象是在MainClass中创建的
A a1 = new A();
A a2 = new A();
A a3 = new A();
Run Code Online (Sandbox Code Playgroud)
在此之后,物体开始过自己的生活.这是我的A类的简化示例.
public class A{
private static volatile String sharedVariable;
Object lockObject = new Object();
private void updateVariable(String newValue){
//... some code
synchronized(lockObject){
//... code to check whether to update the value
sharedVariable = newValue;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我希望synchronized块与所有实例和A类的所有线程同步,我应该将lockObject声明为私有静态volatile吗?如果我使用类(this)同步块,它会完成同样的事情吗?
我认为使用上面的代码,类A的所有实例都创建自己的lockObject并与之同步.因此,只有每个实例(a1,a2和a3)中的线程才会发生同步.它是否正确?