使用原子和无限循环在JAVA中执行同步(Using Atomic and infinite loop to do synchronisation in JAVA)
考虑下面的代码
static AtomicBoolean initialized = new AtomicBoolean(false); static AtomicBoolean initStarted= new AtomicBoolean(false); public static void init() { if (!initialized.get() && !initStarted.getAndSet(true)) { doInitialization(); initialized.set(true); } // start waiting while (!initialized.get()); // finished waiting doMoreStuff(); }
它实现了我想要确保在
doInitialization()
完成之前不会调用doInitialization()
,只有第一个线程应该调用doInitialization()
。我的问题是,这与使用
synchronized
块到整个init()
方法相比如何?正如我所看到的,AtomicReference也使用无限循环(又称繁忙等待)来浪费CPU周期来进行更新(参见
AtomicReference#getAndUpdate()
),所以在这里做同样的方法可能不是那么糟糕吗?如果无限循环如此糟糕(例如,浪费CPU周期),那么为什么AtomicReference使用
synchronized
来停止或唤醒线程呢?Consider the following code
static AtomicBoolean initialized = new AtomicBoolean(false); static AtomicBoolean initStarted= new AtomicBoolean(false); public static void init() { if (!initialized.get() && !initStarted.getAndSet(true)) { doInitialization(); initialized.set(true); } // start waiting while (!initialized.get()); // finished waiting doMoreStuff(); }
It achieves what I want to make sure
doMoreStuff()
is not called untildoInitialization()
has completed and only the very first thread should calldoInitialization()
.My question is, how does this compare to using
synchronized
block to the wholeinit()
method?As I see AtomicReference also use infinite loop (a.k.a busy waiting) to waste CPU cycle to do update (see
AtomicReference#getAndUpdate()
), so may be it is not so bad to do the same here as a synchronisation method?If infinite loop is so bad (e.g. wasting CPU cycle) then why don't AtomicReference use
synchronized
to stop or wake up threads?
原文:https://stackoverflow.com/questions/41051738
最满意答案
我已经解决了我的问题。 它只是改变了
restEx.getResponse().getContent()
成
restEx.getResponse().getContent().toString()
I've fixed my issue. It's only change from
restEx.getResponse().getContent()
into
restEx.getResponse().getContent().toString()
相关问答
更多-
是不是有一款什么手机软件,可以很自然的拉长腿部?不是美图秀秀,有知道的不?[2022-10-20]
spring -
春天的英语怎么读?[2022-05-21]
spring -
angularjs rest以json格式返回xml [duplicate](angularjs rest call return xml in json format [duplicate])[2022-06-26]
您可以直接调用外部URL,并且必须使用第三方库进行xml转换,因为angular不会使用手动http提供转换本身。 我推荐x2js,因为它很简单,很容易理解,例如如何使用它如下所示,如果你想了解更多细节,请浏览文档。 调节器 module.controller('exampleCtrl', function($scope,exampleSvc){ function loadExternalXml(){ var x2js = new X2JS(); example ... -
附录(4/20/16) 匿名用户建议编辑并拒绝,但他的建议看起来很好,有帮助,所以我添加它: 使用Spring Boot 1.3.1.RELEASE 新步骤1 -将以下属性添加到application.properties中是容易且较少侵入的: spring.mvc.throw-exception-if-no-handler-found=true spring.resources.add-mappings=false 比修改现有的DispatcherServlet实例要容易得多(如下所示)! - JO' ...
-
正如其他人所说,你根本不需要mockMVC。 如果要测试REST端点,则需要的是TestRestTemplate。 Runwith SpringRunner.class以及WebEnvironment设置非常重要。 @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT) public class RestServiceApplicationTests { private Stri ...
-
使用Spring-Data-Rest将响应作为XML而不是JSON返回(Use Spring-Data-Rest to return response as XML instead of JSON)[2022-01-18]
我希望在Spring Data REST中生成XML有一个简单的答案,但我还没有找到。 我们默认使用JSON,因为我们认为这是一个超级简单,轻量级的“协议”,用于传输对象,并且在大多数情况下都能正常工作。 XML存在一些问题,我们没有找到合理的答案: 如何用XML表示对象? 我是否使用属性名称作为元素名称,还是使用标准元素名称并将属性名称放在属性中? 我在哪里确定属性的类型(无论是复杂还是简单,Long,BigInteger还是你有什么)? 我是否省去了所有自定义映射信息,只使用JAXB或Spring OX ... -
从您的方法中删除@ResponseStatus 。 它会产生不良副作用而您不需要它,因为您在ResponseEntity中设置了HttpStatus.UNPROCESSABLE_ENTITY 。 来自ResponseStatus上的JavaDoc : 警告:在异常类上使用此批注时,或者在设置此批注的reason属性时,将使用HttpServletResponse.sendError方法。 使用HttpServletResponse.sendError ,响应被认为是完整的,不应再写入。 此外, Servle ...
-
全局异常处理程序中的JSON支持Spring MVC Rest控制器(JSON support in Global Exception Handler Spring MVC Rest controller)[2024-01-21]
这是我的错误。 在xml文件中我没有包含全局异常包 现在我包括了这个包,它工作正常。 It was my mistake. In the xml file I did not include the global exception package for Now I included the package and it is working fine. -
我已经解决了我的问题。 它只是改变了 restEx.getResponse().getContent() 成 restEx.getResponse().getContent().toString() I've fixed my issue. It's only change from restEx.getResponse().getContent() into restEx.getResponse().getContent().toString()
-
Spring的REST(Spring with REST)[2022-02-01]
查看端点映射到的代码 public static final String GET_ALL_EMPLOYEES = "/rest/employee/"; 在哪里访问的网址是 /SpringMVC/rest/employee org.springframework.web.servlet.PageNotFound noHandlerFound警告:在名为“springmvc”的DispatcherServlet中找不到带有URI [/ SpringMVC / rest / employee]的HTTP请求 ...