Java 多线程JaxRs调用-并行流还是未来?
我试图弄清楚通过并行流异步执行同步JAXR是否安全 下面是一个代码示例:Java 多线程JaxRs调用-并行流还是未来?,java,multithreading,parallel-processing,jersey,jax-rs,Java,Multithreading,Parallel Processing,Jersey,Jax Rs,我试图弄清楚通过并行流异步执行同步JAXR是否安全 下面是一个代码示例: public class Singleton { // I'm a Singleton private WebTarget webtarget; public void deleteElement(String id) { try { Response res = this.webtarget.path("/item/{id}").request().buildDelete().invoke()
public class Singleton { // I'm a Singleton
private WebTarget webtarget;
public void deleteElement(String id) {
try {
Response res = this.webtarget.path("/item/{id}").request().buildDelete().invoke();
if (res.getStatusInfo() != Response.Status.OK) {
throw new TechnicalException("distant service failed and responded" + res.getStatus());
}
} catch (Exception e) {
LOGGER.error("error");
throw new TechnicalException("error", e);
}
}
}
在并行流中调用此代码安全吗?或者它不是,我应该使用
submit()
而不是invoke()
来获得未来
,这样我就可以在所有删除之后加入。除了WebTarget
,调用path()
时它会创建一个新的WebTarget
,这是安全的,你认为这样做可能不安全吗?除了线程安全之外,我的问题是,它是否真的被异步处理,还是在前一个submit
解析时,Webtarget
会被锁定。没有锁定。一旦调用webtarget.path()
,就会得到一个新的webtarget
。非常好,谢谢,这回答了我的问题。