最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用synchronized實(shí)現(xiàn)一個(gè)Lock代碼詳解

 更新時(shí)間:2017年12月19日 16:36:16   作者:三 豐  
這篇文章主要介紹了使用synchronized實(shí)現(xiàn)一個(gè)Lock代碼詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下。

剛看到這個(gè)題目的時(shí)候無從下手,因?yàn)橛X得synchronized和lock在加鎖的方式上有很大不同,比如,看看正常情況下synchronized時(shí)如何加鎖的。

方式一:

public synchronized void a(){ 
  //TODO 
} 

方式二:

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

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

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

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

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

然后要想怎么用synchronized去實(shí)現(xiàn)這兩個(gè)方法。

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

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
	}
}

這段代碼我只是加了一點(diǎn)注釋,別的什么都沒有做,是不是幫助理解這段代碼,看看出現(xiàn)頻率最高的詞是什么,是currentthread,那么我們?nèi)ヌ畛鋖ock()和unlock()方法的時(shí)候是不是注意要抓住currentthread這個(gè)關(guān)鍵字就可以找到解決方案呢?答案是肯定的。

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

我們應(yīng)該使用當(dāng)前鎖是否被占用作為判斷條件,如果鎖被占用,currentthread等待,想想我們在使用synchronized的時(shí)候是不是一直使用的這個(gè)條件,答案也是肯定的。

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

現(xiàn)在來看看完整的代碼:

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();
				}
			}
			);
		}
	}
}

運(yùn)行一下看看結(jié)果:

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循環(huán)改成30次,再看一下結(jié)果:

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 

總結(jié)

以上就是本文關(guān)于使用synchronized實(shí)現(xiàn)一個(gè)Lock代碼詳解的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:

Java線程同步Lock同步鎖代碼示例

Java編程synchronized與lock的區(qū)別【推薦】

Java多線程之readwritelock讀寫分離的實(shí)現(xiàn)代碼

如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

相關(guān)文章

  • java使用JMF實(shí)現(xiàn)音樂播放功能

    java使用JMF實(shí)現(xiàn)音樂播放功能

    這篇文章主要為大家詳細(xì)介紹了java使用JMF實(shí)現(xiàn)音樂播放的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • java string類的常用方法詳細(xì)介紹

    java string類的常用方法詳細(xì)介紹

    在開發(fā)過程中經(jīng)常會使用到j(luò)ava string類的方法,本文將以此問題進(jìn)行詳細(xì)介紹
    2012-11-11
  • Java時(shí)間復(fù)雜度、空間復(fù)雜度的深入詳解

    Java時(shí)間復(fù)雜度、空間復(fù)雜度的深入詳解

    對于一個(gè)算法,其時(shí)間復(fù)雜度和空間復(fù)雜度往往是相互影響的,當(dāng)追求一個(gè)較好的時(shí)間復(fù)雜度時(shí),可能會使空間復(fù)雜度的性能變差,即可能導(dǎo)致占用較多的存儲空間,這篇文章主要給大家介紹了關(guān)于Java時(shí)間復(fù)雜度、空間復(fù)雜度的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • SpringBoot整合Retry實(shí)現(xiàn)錯誤重試過程逐步介紹

    SpringBoot整合Retry實(shí)現(xiàn)錯誤重試過程逐步介紹

    重試的使用場景比較多,比如調(diào)用遠(yuǎn)程服務(wù)時(shí),由于網(wǎng)絡(luò)或者服務(wù)端響應(yīng)慢導(dǎo)致調(diào)用超時(shí),此時(shí)可以多重試幾次。用定時(shí)任務(wù)也可以實(shí)現(xiàn)重試的效果,但比較麻煩,用Spring Retry的話一個(gè)注解搞定所有,感興趣的可以了解一下
    2023-02-02
  • Java注解(annotation)簡述

    Java注解(annotation)簡述

    這篇文章主要介紹了使用java的注解(用在java類的方法上的注解)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • Idea里github的圖形化操作配置方法

    Idea里github的圖形化操作配置方法

    這篇文章主要介紹了Idea里github的圖形化操作配置方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • Java從零編寫汽車租賃系統(tǒng)全程分析

    Java從零編寫汽車租賃系統(tǒng)全程分析

    這篇文章介紹了Java實(shí)現(xiàn)汽車租賃系統(tǒng)的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-12-12
  • 淺談Spring Boot 屬性配置和自定義屬性配置

    淺談Spring Boot 屬性配置和自定義屬性配置

    這篇文章主要介紹了淺談Spring Boot 屬性配置和自定義屬性配置,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • SpringBoot工程下Lombok的應(yīng)用教程詳解

    SpringBoot工程下Lombok的應(yīng)用教程詳解

    這篇文章主要給大家介紹了關(guān)于SpringBoot工程下Lombok應(yīng)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Java中的多態(tài)用法實(shí)例分析

    Java中的多態(tài)用法實(shí)例分析

    這篇文章主要介紹了Java中的多態(tài)用法,較為詳細(xì)的分析了java中多態(tài)的概念與相關(guān)的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04

最新評論

塔河县| 宜城市| 黔西县| 隆尧县| 呼玛县| 前郭尔| 恩施市| 故城县| 拜城县| 苏尼特右旗| 定州市| 德兴市| 宣武区| 寿阳县| 岳西县| 洪雅县| 江安县| 眉山市| 祁东县| 华安县| 阜阳市| 望江县| 邳州市| 大厂| 靖远县| 达拉特旗| 于都县| 九台市| 叶城县| 平安县| 钦州市| 武川县| 琼海市| 西城区| 雷州市| 阿拉善右旗| 垫江县| 密山市| 晋宁县| 墨江| 余干县|