java调用线程安全,在Java中方法引用作为方法参数线程安全吗?

博客探讨了在多线程环境下,使用枚举类型的单例`FacesBinding`与方法引用`entity::setId`进行接口绑定是否线程安全的问题。核心在于`setId`方法是否线程安全。方法引用本质上是创建了一个实现接口的匿名内部类,持有`entity`对象的引用,因此,如果`setId`方法本身是线程安全的,那么整个调用也是线程安全的。
摘要由CSDN通过智能技术生成

i have the following scenario:

interface ValueBinding {

public void setValue(T input);

}

public enum FacesBinding {

VALUE;

public void bindString(ValueBinding fcn, HttpServletRequest req, String param){

try {

String val = req.getParameter(param);

if( val != null )

fcn.setValue(val);

} catch (Exception e) { }

}

public void bindBoolean(ValueBinding fcn, HttpServletRequest req, String param){

try {

fcn.setValue(req.getParameter(param) != null);

} catch (Exception e) { }

}

public void bindInt(ValueBinding fcn, HttpServletRequest req, String param){

try {

int val = Integer.parseInt(req.getParameter(param));

fcn.setValue(val);

} catch (Exception e) { }

}

public void bindLong(ValueBinding fcn, HttpServletRequest req, String param){

try {

long val = Long.parseLong(req.getParameter(param));

fcn.setValue(val);

} catch (Exception e) { }

}

...

...

}

and i use it in a "multithreaded" Environment like this:

concurrent Threads are calling this method

@Override // concurrent Threads are calling this method

public Category initData(FacesContext facesContext) throws Exception {

Category entity = new Category();

HttpServletRequest req = facesContext.getRequest();

FacesBinding.VALUE.bindLong(entity::setId, req, Table.Category.Field.ID.name());

FacesBinding.VALUE.bindString(entity::setName, req, Table.Category.Field.NAME.name());

FacesBinding.VALUE.bindInt(entity::setPosition, req, Table.Category.Field.POSITION.name());

FacesBinding.VALUE.bindBoolean(entity::setLocalized, req, Table.Category.Field.LOCALIZED.name());

return entity;

}

is

FacesBinding.VALUE.bindLong(entity::setId, req, Table.Category.Field.ID.name());

100% Thread safe when i pass a method reference(interface) entity::setId as parameter of a method in enum Object (Singleton)?

NOTE:

entity::setId

entity::setName

entity::setPosition

...etc. ALL these methods are standard java setter methods

public void setId(long id) {

this.id = id;

}

public void setName(String name) {

this.name = name;

}

....

UPDATE:

to be concrete:

ARE

Category entity = new Category();

entity.setId(5);//thread safe for sure

100% equal to

FacesBinding.VALUE.bindLong(entity::setId, ...);

does the fact that FacesBinding ist Singleton and the method Reference in bindLong(entity::setId, ...) makes it thread-unsafe??

解决方案

Your method reference call will be thread-safe if your setId method is thread-safe, nothing more and nothing less.

Method reference is a fancy shorthand for creating your ValueBinding objects. When they are compiled, there will be a private inner class of sorts that implements your functional interface and calls the method you specified. Since you specified method that belongs to an object, this inner class will also be equivalent to having constructor that accepts your Category object, and a private field to store it (I said equivalent to because by default implementations are not limited to this behavior if they can justify and choose any other).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值