public void TestSingleton2() { Thread t1, t2; ThreadSingleton s1 = null, s2 = null; t1 = new Thread(() => { s1 = ThreadSingleton.GetInstance(); } ); t2 = new Thread(() => { s2 = ThreadSingleton.GetInstance(); } ); t1.Start(); t2.Start(); t1.Join(); t2.Join(); Assert.IsNotNull(s1); Assert.IsNotNull(s2); Assert.AreNotSame(s1, s2); }
public void SingleInstancePerThead() { Assert.That( ThreadSingleton.Instance(), Is.EqualTo(ThreadSingleton.Instance()) ); }
/// <summary> /// 每个线程的执行部分 /// </summary> public void Procedure() { ThreadSingleton s1 = ThreadSingleton.Instance(); ThreadSingleton s2 = ThreadSingleton.Instance(); //证明可以正常构造实例 Assert.IsNotNull(s1); Assert.IsNotNull(s2); //验证当前线程执行体内两次获取的是同一个实例 Assert.AreEqual(s1.GetHashCode(), s2.GetHashCode()); //记录当前线程所使用对象的HashCode Log.Add(s1.GetHashCode()); }
public void UniqueInstancesPerThead() { ThreadSingleton s1 = null; ThreadSingleton s2 = null; var thread1 = new Thread(() => s1 = ThreadSingleton.Instance()); var thread2 = new Thread(() => s2 = ThreadSingleton.Instance()); thread1.Start(); thread2.Start(); thread1.Join(); thread2.Join(); Assert.That(s1, Is.Not.EqualTo(s2)); }
public static ThreadSingleton GetInstance() { // first checking if (_instance == null) { lock (sync) { // second checking if (_instance == null) { _instance = new ThreadSingleton(); } } } return _instance; }
public ThreadSingleton GetThreadSingleton() { // 当第一个线程运行到这里时,此时会对locker对象 "加锁", // 当第二个线程运行该方法时,首先检测到locker对象为"加锁"状态,该线程就会挂起等待第一个线程解锁 // lock语句运行完之后(即线程运行完之后)会对该对象"解锁" if (_threadSingleton != null) { return(_threadSingleton); } lock (Locker) { if (_threadSingleton == null) { _threadSingleton = new ThreadSingleton(); } } return(_threadSingleton); }
public ActorRegistryHeaderChannel(ActorRegistry registry) { _registry = registry; _match = new ThreadSingleton <MatchHeaderCallback, MatchHeader>(() => new MatchHeaderImpl()); }
public void TheadSingletonNotNull() { var s = ThreadSingleton.Instance(); Assert.NotNull(s); }
public MatchHeaderChannel(HeaderChannel output) { _match = new ThreadSingleton <MatchHeaderChannel, MatchHeader>(() => new MatchHeaderImpl()); _output = output; }