English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Implementation of the Java singleton patternThis article sorts out several implementation methods of the Java singleton pattern:
Singleton pattern is written like this 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; } }
However, in actual development, it will not be written like this because there is a serious problem, when multiple threads access concurrently, it may produce multiple instances!
The following are several commonly used methods:
1.using the synchronized keyword
package singleton; public class SingleTon1 { private SingleTon1{ } private static SingleTon1 instance = null; //Solution to multi-threading problems one, but not very efficient! Because it will lock every time it is called! 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 object = new Object(); //A very clever method, only locking when it is null, and not locking after that public static SingleTon1 getInstance2{ if (instance == null) { synchronized (object) { instance = new SingleTon1()); } } return instance; } }
2.lock
package singleton; import java.util.concurrent.locks.ReentrantLock; public class SingleTon2 { private SingleTon2{ } private static ReentrantLock lock = new ReentrantLock(); 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 that we still need to judge here! instance = new SingleTon2()); } lock.unlock(); } return instance; } }
3Using 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 the class created by singleton pattern static class Nested { private Nested() { } static Nested instance = new Nested(); } }
The following are the commonly used patterns for creating singletons:
Test code:
package singleton; import singleton.SingleTon3.Nested; public class Test2 { public static void main(String[] args) { // TODO Auto-generated method stub Nested singleton; Myrunnable mm = new Myrunnable(); Myrunnable m1 = new Myrunnable(); Myrunnable2 m2 = new Myrunnable2()); new Thread(m1).start(); new Thread(m2).start(); if(m1.singleton){2System.out.println("The same"); //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
Thank you for reading and hope it can help everyone. Thank you for your support of this site!
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been edited by humans, and does not assume relevant legal liabilities. If you find any content suspected of copyright infringement, please send an email to notice#w3Please send an email to codebox.com (replace # with @ when sending email) to report violations, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.