[sometag] here is more text
it spans more than one line [/sometag]
[sometag]this is a bit of text[/sometag]
Use following regexp pattern:
(?s)\[sometag\](.*?)\[\/sometag\]
example
[sometag] here is more text
it spans more than one line [/sometag]
[sometag]this is a bit of text[/sometag]
Use following regexp pattern:
(?s)\[sometag\](.*?)\[\/sometag\]
example
public static Singleton
getInstance() {
if (instance == null) {
synchronized(Singleton.class) {
//1
if (instance == null)
//2 instance = new Singleton();
//3
}
}
return instance;
}
The theory behind double-checked locking is that the second check at //2 makes it impossible for two different Singleton
objects to be created. Consider the following sequence of events:getInstance()
method.synchronized
block at //1 because instance
is null
.getInstance()
method.instance
is still null
. However, because thread 1 holds the lock, thread 2 blocks at //1.null
at //2, creates a Singleton
object and assigns its reference to instance
.synchronized
block and returns instance from the getInstance()
method.instance
is null
.instance
is non-null
, a second Singleton
object is not created and the one created by thread 1 is returned. public class Singleton1 {
private static final Singleton1 instance = new Singleton1();
public static Singleton1 getInstance() {
return instance;
}
}
public class Singleton2 {
private static final Singleton2 instance;
public synchronized static Singleton2 getInstance() {
if (instance == null)
instance = new Singleton2();
return instance;
}
}
In modern JVM these expenses are minimal. public class Singleton3 {
private static class Instance {
static final Singleton3 value = new Singleton3();
}
public static Singleton3 getInstance() {
return Instance.value;
}
}
In that case static inner class will be loaded when getInstance() method will be called.