国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

設(shè)計(jì)模式-單例模式

2019-11-10 23:31:21
字體:
供稿:網(wǎng)友

這是用的最多的一種設(shè)計(jì)模式就不多說了,這里注意一下多線程環(huán)境下的同步問題

java實(shí)現(xiàn)代碼:

package com.liu.pattern5;import java.util.concurrent.locks.ReentrantLock;public class Single {	PRivate static Single single;		private static ReentrantLock lock = new ReentrantLock();		public static Single getInstance(){				lock.lock();		try{						if(single==null){				single = new Single();						}						return single;		}finally{			lock.unlock();					}			}		public static void main(String[] args) {		// TODO Auto-generated method stub		Thread t1 = new MyThread();		Thread t2 = new MyThread();		Thread t3 = new MyThread();		t1.start();		t2.start();		t3.start();	}}class MyThread extends Thread{	public void run(){				Single s = Single.getInstance();			}}

 

c#實(shí)現(xiàn)代碼

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Threading;namespace com.liu.pattern5{    class Single    {        private static Single single;        private static Object _lock = new Object();        public static Single GetInstance()        {            lock (_lock)            {                if (single == null) {                    single = new Single();                }                return single;            }        }        public static void Run(){            System.Console.WriteLine(Thread.CurrentThread.Name);            Single.GetInstance();        }        static void Main(string[] args)        {            Thread t1 = new Thread(new ThreadStart(Run));            Thread t2 = new Thread(new ThreadStart(Run));            t1.Name = "t1";            t2.Name = "t2";            t1.Start();            t2.Start();            t1.Join();            t2.Join();            System.Console.ReadKey();        }    }}

 


上一篇:IO流_IO流概述及分類

下一篇:va_list 詳解

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 新兴县| 类乌齐县| 宁夏| 古丈县| 襄垣县| 达州市| 武平县| 顺昌县| 即墨市| 留坝县| 景宁| 麟游县| 杂多县| 齐齐哈尔市| 沛县| 运城市| 洛川县| 晴隆县| 宣威市| 内丘县| 临沧市| 仪征市| 旬阳县| 澄江县| 精河县| 仙游县| 安西县| 苗栗市| 东源县| 右玉县| 宽城| 哈密市| 宁国市| 海宁市| 达尔| 巨野县| 织金县| 江达县| 宣威市| 宜兰县| 长武县|