對synchronized this 的一些理解

2021-04-12 14:49:14 字數 3795 閱讀 9619

一、當兩個併發執行緒訪問同乙個物件object中的這個synchronized(this)同步**塊時,乙個時間內只能有乙個執行緒得到執行。另乙個執行緒必須等待當前執行緒執行完這個**塊以後才能執行該**塊。

二、然而,當乙個執行緒訪問object的乙個synchronized(this)同步**塊時,另乙個執行緒仍然可以訪問該object中的非synchronized(this)同步**塊。

三、尤其關鍵的是,當乙個執行緒訪問object的乙個synchronized(this)同步**塊時,其他執行緒對object中所有其它synchronized(this)同步**塊的訪問將被阻塞。  

四、第三個例子同樣適用其它同步**塊。也就是說,當乙個執行緒訪問object的乙個synchronized(this)同步**塊時,它就獲得了這個object的物件鎖。結果,其它執行緒對該object物件所有同步**部分的訪問都被暫時阻塞。

五、以上規則對其它物件鎖同樣適用.

舉例說明:

一、當兩個併發執行緒訪問同乙個物件object中的這個synchronized(this)同步**塊時,乙個時間內只能有乙個執行緒得到執行。另乙個執行緒必須等待當前執行緒執行完這個**塊以後才能執行該**塊。

package ths;

public class thread1 implements runnable

} }

public static void main(string args)

} 結果:

a synchronized loop 0

a synchronized loop 1

a synchronized loop 2

a synchronized loop 3

a synchronized loop 4

b synchronized loop 0

b synchronized loop 1

b synchronized loop 2

b synchronized loop 3

b synchronized loop 4

二、然而,當乙個執行緒訪問object的乙個synchronized(this)同步**塊時,另乙個執行緒仍然可以訪問該object中的非synchronized(this)同步**塊。

package ths;

public class thread2 catch (interruptedexception ie)

} }

} public void m4t2() catch (interruptedexception ie)

} }

public static void main(string args)

}, "t1"

); thread t2 = new thread(

new runnable()

}, "t2"

); t1.start();

t2.start();

} }

結果:

t1 : 4

t2 : 4

t1 : 3

t2 : 3

t1 : 2

t2 : 2

t1 : 1

t2 : 1

t1 : 0

t2 : 0

三、尤其關鍵的是,當乙個執行緒訪問object的乙個synchronized(this)同步**塊時,其他執行緒對object中所有其它synchronized(this)同步**塊的訪問將被阻塞。

//修改thread2.m4t2()方法:

public void m4t2() catch (interruptedexception ie)

} }

結果:

t1 : 4

t1 : 3

t1 : 2

t1 : 1

t1 : 0

t2 : 4

t2 : 3

t2 : 2

t2 : 1

t2 : 0

四、第三個例子同樣適用其它同步**塊。也就是說,當乙個執行緒訪問object的乙個synchronized(this)同步**塊時,它就獲得了這個object的物件鎖。結果,其它執行緒對該object物件所有同步**部分的訪問都被暫時阻塞。

//修改thread2.m4t2()方法如下:

public synchronized void m4t2() catch (interruptedexception ie)

} }

結果:

t1 : 4

t1 : 3

t1 : 2

t1 : 1

t1 : 0

t2 : 4

t2 : 3

t2 : 2

t2 : 1

t2 : 0

五、以上規則對其它物件鎖同樣適用:

package ths;

public class thread3 catch(interruptedexception ie)

} }

private void m4t2() catch(interruptedexception ie)

} }

} private void m4t1(inner inner)

} private void m4t2(inner inner)

public static void main(string args)

}, "t1"

); thread t2 = new thread(

new runnable()

}, "t2"

); t1.start();

t2.start();

} }

結果:

儘管執行緒t1獲得了對inner的物件鎖,但由於執行緒t2訪問的是同乙個inner中的非同步部分。所以兩個執行緒互不干擾。

t1 : inner.m4t1()=4

t2 : inner.m4t2()=4

t1 : inner.m4t1()=3

t2 : inner.m4t2()=3

t1 : inner.m4t1()=2

t2 : inner.m4t2()=2

t1 : inner.m4t1()=1

t2 : inner.m4t2()=1

t1 : inner.m4t1()=0

t2 : inner.m4t2()=0

現在在inner.m4t2()前面加上synchronized:

private synchronized void m4t2() catch(interruptedexception ie)

} }

結果:

儘管執行緒t1與t2訪問了同乙個inner物件中兩個毫不相關的部分,但因為t1先獲得了對inner的物件鎖,所以t2對inner.m4t2()的訪問也被阻塞,因為m4t2()是inner中的乙個同步方法。

t1 : inner.m4t1()=4

t1 : inner.m4t1()=3

t1 : inner.m4t1()=2

t1 : inner.m4t1()=1

t1 : inner.m4t1()=0

t2 : inner.m4t2()=4

t2 : inner.m4t2()=3

t2 : inner.m4t2()=2

t2 : inner.m4t2()=1

t2 : inner.m4t2()=0 

對建立游標對理解

步驟 1 首先經過底層的 編寫已經建立起了了python和資料庫之間的溝通橋梁並封裝成了模組,通過pymysql下的conntent 方法與之建立起了連線,例項物件。pymysql.connect 方法返回的是connections模組下的connection 例項,connect 方法傳參就是在給...

對勾函式 對勾函式

對勾函式是一種類似於反比例函式的一般函式。所謂的對勾函式,是形如 f x ax b x 的函式,是 一種教材上沒有但考試老喜歡考的函式,所以更加要注意和學習。一般的函式影象形似兩個中心對稱的 對勾,故名。當x 0 時,f x ax b x 有最小值 這裡為了研究方便,規定a 0 b 0 也就是當 x...

一對多 多對一

pip install djangorestframework 3.9.2 pip install markdown 3.0.1 markdown support for the browsable api.pip install django filter 2.1.0 filtering supp...