Questions tagged [double-checked-locking]

Double-checked locking is a software design pattern used to reduce the overhead of acquiring a lock by first testing the locking criterion without actually acquiring the lock.

From Wikipedia:

In software engineering, double-checked locking (also known as "double-checked locking optimization"1) is a software design pattern used to reduce the overhead of acquiring a lock by first testing the locking criterion (the "lock hint") without actually acquiring the lock. Only if the locking criterion check indicates that locking is required does the actual locking logic proceed.

The pattern, when implemented in some language/hardware combinations, can be unsafe. At times, it can be considered an anti-pattern.[2]

It is typically used to reduce locking overhead when implementing "lazy initialization" in a multi-threaded environment, especially as part of the Singleton pattern. Lazy initialization avoids initializing a value until the first time it is accessed.

143 questions
0
votes
0 answers

Double checked locking with neither volatile nor a local variable

Before dismissing this, it is possible to implement double checked locking without volatile, see below. I'm suggesting a variation on this, that gets rid of the local variable. The following is a correct implementation of double checked locking from…
Roland
  • 7,525
  • 13
  • 61
  • 124
0
votes
7 answers

Double-checked locking for growable array of binomial coefficients

I'm trying to use double-checked locking to maintain an array of binomial coefficients, but I read recently that double-checked locking doesn't work. Efficiency is extremely important so using volatile isn't an option unless it's only inside the…
0
votes
2 answers

java codes that fails double locking checking

Notes: I know that before java 5(given in 2004), double checking lock will fail in java, even you add "volatile" to the "instance" field. and after java 5 the volatile semantics has been right for double checking lock. I also know that without…
ZhaoGang
  • 4,491
  • 1
  • 27
  • 39
0
votes
1 answer

Does the latest JMM specify the synchronized block to be atomic to other threads even asynchronized ones?

As I went through an article about DOUBLE-CHECKED LOCKING on http://www.javaworld.com/article/2074979/java-concurrency/double-checked-locking--clever--but-broken.html , I encounter a comment which says " It should be noted that DCL might, in fact,…
0
votes
1 answer

DCL with two synchronized block is broken?

I could not understand below code snippet from A fix that doesn't work. (I did read the explanation that follows on same page). If we have 2 synchronized blocks, how is this DCL version broken? Or is it not applicable post Java5? // (Still) Broken…
ADJ
  • 1,182
  • 2
  • 14
  • 26
0
votes
2 answers

Java double checked locking - Strings

Given that strings contain final field, does it mean in the context of double checked locking it is not necessary to declare them volatile? E.g. class SomeClass{ private String val; String getVal(){ if(val == null){ …
Bober02
  • 15,034
  • 31
  • 92
  • 178
0
votes
1 answer

Thread safety while access Db in spring bean

I have a singleton spring service that is being called by spring rest controller. The singleton service MyService has some method addRecordIfNotExistsBefore, which has the following implementation: public void addRecordIfNotExistsBefore(String…
Muhammad Hewedy
  • 29,102
  • 44
  • 127
  • 219
0
votes
1 answer

Is this a correct use of double checked singleton?

I am basically looking for a caching mechanism for the users of HelperWrapper. Is this a correct use of double checked singleton? final class HelperWrapper {   private static volatile Helper helper = null;     public static Helper getHelper()…
Ares
  • 1,411
  • 1
  • 19
  • 35
0
votes
1 answer

Why to double check the singleton instantiation

In this link i found the singleton instantiation as below: public static Singleton getInstanceDC() { if (_instance == null) { // Single Checked (1) synchronized (Singleton.class) { if (_instance ==…
jayendra bhatt
  • 1,337
  • 2
  • 19
  • 41
0
votes
0 answers

PreApplicationStartMethod called multiple times

A method in a class in an assembly decorated by PreApplicationStartMethod attribute is meant to be called before Application_Start event is fired. For example, this is how BuildManager gets to know what assemblies (in addition to ones in the web…
Alexander Christov
  • 9,625
  • 7
  • 43
  • 58
0
votes
1 answer

Performance test:singleton class with and without double check locking

I have two implementations of singleton classes public class Test2 { private static Test2 _instance=new Test2(); private Test2(){ } public static synchronized Test2 getInstance(){ if(_instance == null){ _instance = new…
0
votes
2 answers

Double mapping with HashMap

We've been working on a piece of code for getting singeltons. We try to get them, if the class exists in our collection we return it otherwise we create it and store it in our collection. We use double-checked locking to make sure that we don't sync…
0
votes
1 answer

How to make this code thread-safe using double checked locking?

Single threaded version: private final List list = new ArrayList(); public Element getElementAt(int index) { if (index >= list.size()) { for (int i = list.size(); i <= index; i++) { …
ZhekaKozlov
  • 36,558
  • 20
  • 126
  • 155
0
votes
1 answer

Implementing Read-Write Locks with Double-Checked Locking

I've written a Java ReadWriteLock where the readers use double-checked locking to acquire the write-lock. Is this unsafe (as is the case for DCL with lazy-instantiation)? import java.util.concurrent.atomic.AtomicInteger; public class DCLRWLock { …
dspyz
  • 5,280
  • 2
  • 25
  • 63
0
votes
1 answer

Looking for a test to reproduce broken double checked locking

Does anybody know of a test that reproduces "broken double checked locking" problem in java?
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
1 2 3
9
10