Several ways to implement Java single example pattern
Single case mode It's written in many books:
public class SingleTon1 {
private SingleTon1 () {
}
private static SingleTon1 instance = null;
public static SingleTon1 getinstance () {
if (instance = null) {
instance = new SingleTon1 ();
}
return instance
}
}
But the actual development is not so written, because there is a serious problem, multithreading concurrent access, may produce multiple instances!!
Here are a few common methods:
1. Use the Synchronized keyword
Package Singleton;
public class SingleTon1 {
private SingleTon1 () {
}
private static SingleTon1 instance = null;
Multithreading problem solution one, but not high efficiency! Because every call will lock! Public
static synchronized SingleTon1 getinstance () {
if (instance = = null) {
instance = new SingleTon1 ();
return
instance;
}
public void print () {
System.out.println ("thread_id:" +thread.currentthread (). GetId ());
}
private static Object = new Object ();
The ingenious method is to add a lock on null and then not add public
static SingleTon1 GetInstance2 () {
if (instance = null) {
Synchronized (object) {
instance = new SingleTon1 ();
}
}
return instance
}
}
2. Add lock
Package Singleton;
Import Java.util.concurrent.locks.ReentrantLock;
public class SingleTon2 {
private SingleTon2 () {
}
private static Reentrantlock lock = new Reentrantlock (); C6/>private static SingleTon2 instance = null;
public void print () {
System.out.println ("thread_id:" +thread.currentthread (). GetId ());
}
public static SingleTon2 GetInstance2 () {
if (instance = null) {
lock.lock ();
if (instance = null) {//Note here also to judge!!
instance = new SingleTon2 ();
}
Lock.unlock ();
}
return instance
}
}
3. Using static variables:
Package Singleton;
public class SingleTon3 {public
static void print () {
System.out.println ("thread_id:" +thread.currentthread () . GetId ());
public static Nested getnested () {return
nested.instance;
}
This is a single instance of the class static classes created
nested{
private Nested () {
}
static Nested instance = new Nested ();
}
}
These are the common patterns for creating a single example:
Test code:
package singleton; Import Singleton.
singleton3.nested; public class Test2 {public static void main (string[] args) {//TODO auto-generated a stub Nested single
ton
myrunnable mm = new myrunnable ();
Myrunnable m1 = new myrunnable ();
Myrunnable2 m2 = new Myrunnable2 ();
New Thread (M1). Start ();
New Thread (M2). Start ();
if (M1.singleton = = M2.singleton) {//Is the same System.out.println ("is the same");
}else{System.out.println ("not the same");
}} class Myrunnable implements runnable{Nested Singleton;
@Override public void Run () {//TODO auto-generated method Stub singleton = singleton3.getnested ();
Singleton3.print ();
} class Myrunnable2 implements runnable{Nested Singleton;
@Override public void Run () {//TODO auto-generated method Stub singleton = singleton3.getnested ();
Singleton3.print (); }
}
Output:
is the same
Thread_id:11
Thread_id:10
The above is the Java single example mode of data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!