synchronized this 的意思是

2021-06-19 19:23:44 字數 3990 閱讀 5700

一、當兩個併發執行緒訪問同乙個物件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

synchronized this 的幾個簡單示例

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

多執行緒之synchronized(this)

使用情景 我們有時候會使用多執行緒但是我們需要在呼叫某乙個例項的時候,我們需要有序,比如,賣火車票的時候,我們要先判斷是否有票,然後再 票,當多個視窗賣火車票的時候,票池就會出錯。會出現一張票被多個售票視窗重複 使用方法 我們可以使用synchronized this 來給票池枷鎖,保證買票的時候,...

對synchronized this 的一些理解

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