常见的创建线程安全单例模式的方法
2018-03-23 16:55
232 查看
1,同步代码块结合双检查锁机制实现单例
package com.thread; public class Singleton { private static Singleton instance = null; /** * 同步代码块结合双检查锁机制实现单例 * * @return */ public static Singleton getInstance() { try { if (instance == null) { synchronized (Singleton.class) { if (instance == null) { // 创建实例之前可能会有一些准备性的耗时工作 Thread.sleep(300); instance = new Singleton(); } } } } catch (InterruptedException e) { e.printStackTrace(); } return instance; } }
package com.thread; public class Singleton { private static Singleton instance = null; /** * 同步代码块结合双检查锁机制实现单例 * * @return */ public static Singleton getInstance() { try { if (instance == null) { synchronized (Singleton.class) { if (instance == null) { // 创建实例之前可能会有一些准备性的耗时工作 Thread.sleep(300); instance = new Singleton(); } } } } catch (InterruptedException e) { e.printStackTrace(); } return instance; } }
package com.thread; public class Test_1 implements Runnable { @Override public void run() { System.out.println(Singleton.getInstance().hashCode()); } public static void main(String[] args) { for(int i=0;i<10;i++) { Thread t = new Thread(new Test_1()); t.start(); } } }
测试结果,说明是同一个对象
2,同步方法实现单例
package com.thread; public class Singleton_2 { private static Singleton_2 instance = null; /** * 同步方法实现线程安全的单例 * * @return */ public static synchronized Singleton_2 getInstance() { try { if (instance == null) { Thread.sleep(300); instance = new Singleton_2(); } } catch (InterruptedException e) { e.printStackTrace(); } return instance; } }
package com.thread; public class Test_1 implements Runnable { @Override public void run() { System.out.println(Singleton_2.getInstance().hashCode()); } public static void main(String[] args) { for(int i=0;i<10;i++) { Thread t = new Thread(new Test_1()); t.start(); } } }
测试结果,线程安全
3,静态代码块package com.thread;
public class Singleton_3 {
private static Singleton_3 instance = null;
static {
instance = new Singleton_3();
}
public static Singleton_3 getInstance() {
return instance;
}
}
package com.thread; public class Test_1 implements Runnable { @Override public void run() { System.out.println(Singleton_3.getInstance().hashCode()); } public static void main(String[] args) { for(int i=0;i<10;i++) { Thread t = new Thread(new Test_1()); t.start(); } } }
4,静态内部类package com.thread;
public class Singleton_4 {
private static class SingletonHandle{
private static Singleton_4 instance = new Singleton_4();
}
public static Singleton_4 getInstance() {
return SingletonHandle.instance;
}
}
package com.thread; public class Test_1 implements Runnable { @Override public void run() { System.out.println(Singleton_4.getInstance().hashCode()); } public static void main(String[] args) { for(int i=0;i<10;i++) { Thread t = new Thread(new Test_1()); t.start(); } } }
测试结果
相关文章推荐
- 线程安全的单例模式的几种实现方法分享
- 线程安全的单例模式的几种实现方法分享
- java创建线程常见的2种方法
- 线程安全的单例模式的几种实现方法分享
- 线程安全的单例模式的几种实现方法分享
- 线程安全的单例模式还需要对成员变量的set get方法设置锁么
- java 创建线程的三种方法Callable,Runnable,Thread比较及用法
- 常见的在Python中实现单例模式的三种方法
- winform中console.write的使用、线程的产生、线程的使用、创建单例模式
- SimpleDateFormat线程不安全示例及其解决方法
- java创建线程的两种方法区别
- java多线程(1):线程的创建和多线程的安全问题
- SE高阶(4):多线程(并发)—①创建启动方式和控制线程方法
- ACE中创建线程的方法
- 2-5-Java多线程-创建线程的Runnable接口方法及Thread源码解析
- 线程安全且按需构建的单例模式
- 创建模式之二:工厂方法(Factory Method) 续2
- VC++ 数个创建线程方法的不同点
- Callable,Runnable比较及用法以及创建线程的4种方法
- Java创建线程的两个方法