设计模式(三)- Singleton模式续(多线程模式下的Singleton模式)
2006-09-12 17:39
302 查看
当为多线程的应用程序创建singleton设计模式时,需要确保它们都是线程安全的。
1Imports System.Threading
2
3Public Class Singleton_threadClass Singleton_thread
4
5 'private member
6 Private Shared m_Instance As Singleton_thread
7 Private Shared m_TimeOfBirth As String
8 Private Shared m_Mutex As New Mutex
9 Private Sub New()Sub New()
10 Console.WriteLine(String.Format("Creating Singleton at {0}", Now.ToLongTimeString))
11 m_TimeOfBirth = Now.ToLongTimeString
12 End Sub
13
14 'public member
15 Public Shared Function GetInstance()Function GetInstance() As Singleton_thread
16 m_Mutex.WaitOne()
17 If m_Instance Is Nothing Then
18 m_Instance = New Singleton_thread
19 End If
20 m_Mutex.ReleaseMutex()
21 Return m_Instance
22 End Function
23
24
25
26End Class
1Imports System.Threading
2
3Public Class Singleton_threadClass Singleton_thread
4
5 'private member
6 Private Shared m_Instance As Singleton_thread
7 Private Shared m_TimeOfBirth As String
8 Private Shared m_Mutex As New Mutex
9 Private Sub New()Sub New()
10 Console.WriteLine(String.Format("Creating Singleton at {0}", Now.ToLongTimeString))
11 m_TimeOfBirth = Now.ToLongTimeString
12 End Sub
13
14 'public member
15 Public Shared Function GetInstance()Function GetInstance() As Singleton_thread
16 m_Mutex.WaitOne()
17 If m_Instance Is Nothing Then
18 m_Instance = New Singleton_thread
19 End If
20 m_Mutex.ReleaseMutex()
21 Return m_Instance
22 End Function
23
24
25
26End Class
相关文章推荐
- Linux 多线程 ”一写多读” 模式下的无锁设计
- 设计模式------Java多线程之静态代理模式
- 架构师入门笔记四 初识多线程设计模式
- 【编程素质】多线程编程之设计模式
- 23种设计模式——Singleton模式
- 面向对象设计模式学习---Singleton模式(创建型)
- 多线程设计模式之Future模式
- 【设计模式】Singleton模式C++实现
- JAVA之旅(十四)——静态同步函数的锁是class对象,多线程的单例设计模式,死锁,线程中的通讯以及通讯所带来的安全隐患,等待唤醒机制
- Day25 --多线程(下) 设计模式 GUI
- 设计模式_Java_多线程之单例设计模式(饿汉式和懒汉式)
- Java设计模式之单例模式(singleton模式)
- 设计模式总结:单例模式(以及多线程、无序写入、volatile对单例的影响)
- java多线程12种设计模式
- 多线程、单例设计模式、死锁
- [设计模式]单例模式(考虑多线程)
- Java 多线程下的单例模式的设计
- 多线程编程的设计模式 不变模式(一)
- Java多线程之并发协作生产者消费者设计模式
- 设计模式-Singleton模式(只有一个实例)