亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 開發 > Java > 正文

使用synchronized實現一個Lock代碼詳解

2024-07-13 10:14:59
字體:
來源:轉載
供稿:網友

剛看到這個題目的時候無從下手,因為覺得synchronized和lock在加鎖的方式上有很大不同,比如,看看正常情況下synchronized時如何加鎖的。

方式一:

public synchronized void a(){   //TODO } 

方式二:

public void b(){   synchronized(this){     //TODO   } } 

從這兩種方式來看,鎖都是加在{}之間的,我們再來看看Lock是如何做的呢:

public void c() {   lock.lock();   try {     // TODO   } finally {     lock.unlock();   } } 

這種方式的鎖是加在lock()和unlock()之間的,所以要想實現一個lock功能,就要想怎么實現這樣兩個方法,lock()和unlock()方法,先定義一個框架如下所示:

public void lock(){}public void unlock(){}

然后要想怎么用synchronized去實現這兩個方法。

現在其實只是稍微清楚了一點思路,但是還不知道怎么去填充這兩個方法,這是后再來分析一下Lock的加鎖有什么特點,再來看看這段代碼:

public void c() {	lock.lock();	//When current thread get the lock, other thread has to wait 	try {		//current thread get in the lock, other thread can not get in 		// TODO	}	finally {		lock.unlock();		//current thread release the lock	}}

這段代碼我只是加了一點注釋,別的什么都沒有做,是不是幫助理解這段代碼,看看出現頻率最高的詞是什么,是currentthread,那么我們去填充lock()和unlock()方法的時候是不是注意要抓住currentthread這個關鍵字就可以找到解決方案呢?答案是肯定的。

接著分析,使用synchronized的時候如何讓線程等待呢?是用wait()方法。怎么讓線程喚醒呢,是用notify()方法。那么就要在lock()方法中使用wait()方法,在unlock()方法中使用notify()方法。那么我們在使用wait()和notify()的時候是有一個條件的,想想我們應該使用什么作為條件呢?

我們應該使用當前鎖是否被占用作為判斷條件,如果鎖被占用,currentthread等待,想想我們在使用synchronized的時候是不是一直使用的這個條件,答案也是肯定的。

再來分析一下什么時候釋放鎖,使用什么作為條件,想想如果線程A拿到了鎖,線程B能釋放嗎?當然不能,如果B能釋放就違反了原則,當然不能??隙ㄊ茿線程的鎖只能A來釋放。所以判斷條件就是判斷持有鎖的線程是不是currentthread,如果是的話,可以釋放,不是的話當然不能。

現在來看看完整的代碼:

package test.lock;import java.util.Random;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.ThreadFactory;public class NaiveLock {	private static final long NONE = -1;	private long owner = NONE;	private Boolean isLooked() {		return owner != NONE;	}	public synchronized void lock() {		long currentThreadId = Thread.currentThread().getId();		if (owner == currentThreadId) {			throw new IllegalStateException("Lock has been acquired by current thread");		}		while (this.isLooked()) {			System.out.println(String.format("thread %s is waitting lock", currentThreadId));			try {				wait();			}			catch (InterruptedException e) {				e.printStackTrace();			}		}		owner = currentThreadId;		System.out.println(String.format("Lock is acquired by thread %s", owner));	}	public synchronized void unlock() {		if (!this.isLooked() || owner != Thread.currentThread().getId()) {			throw new IllegalStateException("Only Lock owner can unlock the lock");		}		System.out.println(String.format("thread %s is unlocking", owner));		System.out.println();		owner = NONE;		notify();	}	public static void main(String[] args) {		final NaiveLock lock = new NaiveLock();		ExecutorService executor = Executors.newFixedThreadPool(20, new ThreadFactory() {			private ThreadGroup group = new ThreadGroup("test thread group");			{				group.setDaemon(true);			}			@Override 			      public Thread newThread(Runnable r) {				return new Thread(group, r);			}		}		);		for (int i = 0; i < 20; i++) {			executor.submit(new Runnable() {				@Override 				        public void run() {					lock.lock();					System.out.println(String.format("thread %s is running...", Thread.currentThread().getId()));					try {						Thread.sleep(new Random().nextint(1000));					}					catch (InterruptedException e) {						e.printStackTrace();					}					lock.unlock();				}			}			);		}	}}

運行一下看看結果:

Lock is acquired by thread 8 thread 8 is running... thread 27 is waitting lock thread 26 is waitting lock thread 25 is waitting lock thread 24 is waitting lock thread 23 is waitting lock thread 22 is waitting lock thread 21 is waitting lock thread 20 is waitting lock thread 19 is waitting lock thread 18 is waitting lock thread 17 is waitting lock thread 16 is waitting lock thread 15 is waitting lock thread 14 is waitting lock thread 13 is waitting lock thread 12 is waitting lock thread 11 is waitting lock thread 10 is waitting lock thread 9 is waitting lock thread 8 is unlocking  Lock is acquired by thread 27 thread 27 is running... thread 27 is unlocking  Lock is acquired by thread 26 thread 26 is running... thread 26 is unlocking  Lock is acquired by thread 25 thread 25 is running... thread 25 is unlocking  Lock is acquired by thread 24 thread 24 is running... thread 24 is unlocking  Lock is acquired by thread 23 thread 23 is running... thread 23 is unlocking  Lock is acquired by thread 22 thread 22 is running... thread 22 is unlocking  Lock is acquired by thread 21 thread 21 is running... thread 21 is unlocking  Lock is acquired by thread 20 thread 20 is running... thread 20 is unlocking  Lock is acquired by thread 19 thread 19 is running... thread 19 is unlocking  Lock is acquired by thread 18 thread 18 is running... thread 18 is unlocking  Lock is acquired by thread 17 thread 17 is running... thread 17 is unlocking  Lock is acquired by thread 16 thread 16 is running... thread 16 is unlocking  Lock is acquired by thread 15 thread 15 is running... thread 15 is unlocking  Lock is acquired by thread 14 thread 14 is running... thread 14 is unlocking  Lock is acquired by thread 13 thread 13 is running... thread 13 is unlocking  Lock is acquired by thread 12 thread 12 is running... thread 12 is unlocking  Lock is acquired by thread 11 thread 11 is running... thread 11 is unlocking  Lock is acquired by thread 10 thread 10 is running... thread 10 is unlocking  Lock is acquired by thread 9 thread 9 is running... thread 9 is unlocking 

如果把for循環改成30次,再看一下結果:

Lock is acquired by thread 8 thread 8 is running... thread 27 is waitting lock thread 26 is waitting lock thread 25 is waitting lock thread 24 is waitting lock thread 23 is waitting lock thread 22 is waitting lock thread 21 is waitting lock thread 20 is waitting lock thread 19 is waitting lock thread 18 is waitting lock thread 17 is waitting lock thread 16 is waitting lock thread 15 is waitting lock thread 14 is waitting lock thread 13 is waitting lock thread 12 is waitting lock thread 11 is waitting lock thread 10 is waitting lock thread 9 is waitting lock thread 8 is unlocking  Lock is acquired by thread 27 thread 27 is running... thread 8 is waitting lock thread 27 is unlocking  Lock is acquired by thread 27 thread 27 is running... thread 26 is waitting lock thread 27 is unlocking  Lock is acquired by thread 27 thread 27 is running... thread 25 is waitting lock thread 27 is unlocking  Lock is acquired by thread 24 thread 24 is running... thread 27 is waitting lock thread 24 is unlocking  Lock is acquired by thread 23 thread 23 is running... thread 24 is waitting lock thread 23 is unlocking  Lock is acquired by thread 22 thread 22 is running... thread 23 is waitting lock thread 22 is unlocking  Lock is acquired by thread 22 thread 22 is running... thread 21 is waitting lock thread 22 is unlocking  Lock is acquired by thread 22 thread 22 is running... thread 20 is waitting lock thread 22 is unlocking  Lock is acquired by thread 22 thread 22 is running... thread 19 is waitting lock thread 22 is unlocking  Lock is acquired by thread 22 thread 22 is running... thread 18 is waitting lock thread 22 is unlocking  Lock is acquired by thread 17 thread 17 is running... thread 17 is unlocking  Lock is acquired by thread 16 thread 16 is running... thread 16 is unlocking  Lock is acquired by thread 15 thread 15 is running... thread 15 is unlocking  Lock is acquired by thread 14 thread 14 is running... thread 14 is unlocking  Lock is acquired by thread 13 thread 13 is running... thread 13 is unlocking  Lock is acquired by thread 12 thread 12 is running... thread 12 is unlocking  Lock is acquired by thread 11 thread 11 is running... thread 11 is unlocking  Lock is acquired by thread 10 thread 10 is running... thread 10 is unlocking  Lock is acquired by thread 9 thread 9 is running... thread 9 is unlocking  Lock is acquired by thread 8 thread 8 is running... thread 8 is unlocking  Lock is acquired by thread 26 thread 26 is running... thread 26 is unlocking  Lock is acquired by thread 25 thread 25 is running... thread 25 is unlocking  Lock is acquired by thread 27 thread 27 is running... thread 27 is unlocking  Lock is acquired by thread 24 thread 24 is running... thread 24 is unlocking  Lock is acquired by thread 23 thread 23 is running... thread 23 is unlocking  Lock is acquired by thread 21 thread 21 is running... thread 21 is unlocking  Lock is acquired by thread 20 thread 20 is running... thread 20 is unlocking  Lock is acquired by thread 19 thread 19 is running... thread 19 is unlocking  Lock is acquired by thread 18 thread 18 is running... thread 18 is unlocking 

總結

以上就是本文關于使用synchronized實現一個Lock代碼詳解的全部內容,希望對大家有所幫助。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美精品久久久久久久| 日韩一区二区精品视频| 日韩动漫免费观看电视剧高清| 欧美亚洲国产另类| 亚洲aⅴ日韩av电影在线观看| 亚洲美女av在线播放| 91探花福利精品国产自产在线| 欧美成年人视频网站欧美| 亚洲成色999久久网站| 欧美国产欧美亚洲国产日韩mv天天看完整| 国产欧美韩国高清| 亚洲一区二区免费在线| 久久久亚洲福利精品午夜| 国产91在线高潮白浆在线观看| 一区二区三区视频观看| 国产精品吹潮在线观看| 亚洲永久在线观看| 亚洲自拍av在线| 久久久这里只有精品视频| 疯狂做受xxxx高潮欧美日本| 日韩在线观看免费高清完整版| 亚洲国产91色在线| 国产ts人妖一区二区三区| 超碰91人人草人人干| 2020国产精品视频| 日韩在线视频线视频免费网站| 好吊成人免视频| 精品人伦一区二区三区蜜桃免费| 精品亚洲精品福利线在观看| 尤物yw午夜国产精品视频| 91精品国产91久久久久久不卡| 日韩视频免费大全中文字幕| 欧美成人精品一区二区三区| 亚洲最大成人在线| 久久伊人精品天天| 亚洲网站在线播放| 日韩欧美亚洲范冰冰与中字| 91久久精品久久国产性色也91| 97av在线播放| 亚洲伊人一本大道中文字幕| 久热精品视频在线免费观看| 欧美一级大片视频| 国产欧美日韩中文字幕在线| 亚洲九九九在线观看| 欧美巨乳在线观看| 欧美www在线| 91在线高清视频| 国产成人啪精品视频免费网| 欧美精品久久一区二区| 国产精品美女在线观看| 亚洲男人第一av网站| 色婷婷**av毛片一区| 亚洲九九九在线观看| 57pao国产精品一区| 秋霞av国产精品一区| 韩日欧美一区二区| 成人激情视频小说免费下载| 操日韩av在线电影| 国产精品视频自在线| 亚洲精品电影在线观看| 亚洲二区中文字幕| 性欧美长视频免费观看不卡| 亚洲精品之草原avav久久| 日韩欧美成人精品| 成人国产精品日本在线| 欧美激情亚洲激情| 日韩美女在线观看一区| 日韩国产欧美区| 国产成人久久久精品一区| 91久久在线播放| 亚洲色图在线观看| 91九色综合久久| 久久久久亚洲精品成人网小说| 国产手机视频精品| 91精品久久久久久久久久久久久久| 97国产精品视频人人做人人爱| 欧美国产日韩一区二区| 国产精品私拍pans大尺度在线| 亚洲女性裸体视频| 成人免费淫片视频软件| 久久综合电影一区| 国产视频精品久久久| 精品国产成人在线| 国产精品欧美一区二区三区奶水| 国产精品99久久久久久白浆小说| 国产精品欧美激情在线播放| 中文字幕在线看视频国产欧美在线看完整| 欧美体内谢she精2性欧美| 欧美大成色www永久网站婷| 国产精品久久久久久久久久久久久久| 精品视频在线播放| 国产精品一二区| 国产精品入口免费视| 热久久这里只有精品| 久久成人人人人精品欧| 国产成人精品在线观看| 波霸ol色综合久久| 国产美女久久精品香蕉69| 欧美三级欧美成人高清www| 日韩av色在线| 尤物99国产成人精品视频| 亚洲成人在线视频播放| 欧美性xxxxxxx| 亚洲人成电影网站色xx| 91理论片午午论夜理片久久| 久久露脸国产精品| 国产网站欧美日韩免费精品在线观看| 日韩精品久久久久久福利| 国产一区玩具在线观看| 欧美精品久久一区二区| 亚洲片国产一区一级在线观看| 91精品国产综合久久久久久久久| 国产日韩在线精品av| 一区二区三区动漫| 91av免费观看91av精品在线| 国产日韩欧美在线视频观看| 一级做a爰片久久毛片美女图片| 欧美小视频在线| 国产日韩亚洲欧美| 91夜夜揉人人捏人人添红杏| 韩剧1988在线观看免费完整版| 久久精品国产欧美亚洲人人爽| 久久久久日韩精品久久久男男| 97视频在线看| 91久久久久久久一区二区| 久久精品视频网站| 在线播放日韩专区| 一区二区三区在线播放欧美| 国产欧美一区二区三区在线| 国产欧美日韩免费| 欧美人与物videos| 美女久久久久久久久久久| 日韩中文字幕av| 秋霞成人午夜鲁丝一区二区三区| 欧洲精品在线视频| 欧美激情视频播放| 黄网站色欧美视频| 成人精品一区二区三区| 欧美性受xxxx黑人猛交| 欧美一级高清免费| 国产69精品99久久久久久宅男| 亚洲福利视频网| 国产999精品久久久影片官网| 亚洲成人激情在线观看| 欧美特黄级在线| 日韩电影在线观看免费| 日韩不卡中文字幕| 欧美成人免费全部观看天天性色| 一夜七次郎国产精品亚洲| 久久久久久久色| 日韩精品在线第一页| 国产精品三级在线| 久久国产精品久久精品| 日韩在线免费高清视频| 欧美性高潮床叫视频| 亚洲欧美激情视频| 久久躁狠狠躁夜夜爽| 欧美性20hd另类| 亚洲国产中文字幕在线观看| 欧美精品久久久久| 韩国日本不卡在线| 欧美日韩国产中字| 国产中文字幕亚洲| 亚洲成**性毛茸茸|