2019-08-16 Synchronized的使用

為什么要使用Synchronized關(guān)鍵字?為了解決線程高并發(fā)安全問題,共享數(shù)據(jù),多線程共同操作共享數(shù)據(jù),Synchronized可以保證同一時(shí)刻只有一個(gè)線程訪問代碼塊或者方法。

結(jié)論:當(dāng)兩個(gè)線程同時(shí)對(duì)一個(gè)對(duì)象的一個(gè)方法進(jìn)行操作,只有一個(gè)線程能夠搶到鎖。因?yàn)橐粋€(gè)對(duì)象只有一把鎖,一個(gè)線程獲取了該對(duì)象的鎖之后,其他線程無法獲取該對(duì)象的鎖,就不能訪問該對(duì)象的其他synchronized實(shí)例方法,但是可以訪問非synchronized修飾的方法

例題:

1、一個(gè)線程獲取了該對(duì)象的鎖之后,其他線程來訪問其他synchronized實(shí)例方法現(xiàn)象:

public synchronized void method1() {

? ? ? ? System.out.println("Method 1 start");

? ? ? ? try {

? ? ? ? ? ? System.out.println("Method 1 execute");

? ? ? ? ? ? Thread.sleep(3000);

? ? ? ? } catch (InterruptedException e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ? System.out.println("Method 1 end");

? ? }

? ? public synchronized void method2() {

? ? ? ? System.out.println("Method 2 start");

? ? ? ? try {

? ? ? ? ? ? System.out.println("Method 2 execute");

? ? ? ? ? ? Thread.sleep(1000);

? ? ? ? } catch (InterruptedException e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ? System.out.println("Method 2 end");

? ? }

? ? ? ? public static void main(String[] args) {

? ? ? ? ? ? final syncTest test = new syncTest();

? ? ? ? ? ? new Thread(new Runnable() {

? ? ? ? ? ? ? ? @Override

? ? ? ? ? ? ? ? public void run() {

? ? ? ? ? ? ? ? ? ? test.method1();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }).start();

? ? ? ? ? ? new Thread(new Runnable() {

? ? ? ? ? ? ? ? @Override

? ? ? ? ? ? ? ? public void run() {

? ? ? ? ? ? ? ? ? ? test.method2();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }).start();

? ? ? ? }

輸出結(jié)果:


2、一個(gè)線程獲取了該對(duì)象的鎖之后,其他線程來訪問其他非synchronized實(shí)例方法現(xiàn)象:

去掉②中方法二的synchronized:

public synchronized void method1() {

? ? ? ? System.out.println("Method 1 start");

? ? ? ? try {

? ? ? ? ? ? System.out.println("Method 1 execute");

? ? ? ? ? ? Thread.sleep(3000);

? ? ? ? } catch (InterruptedException e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ? System.out.println("Method 1 end");

? ? }

? ? public void method2() {

? ? ? ? System.out.println("Method 2 start");

? ? ? ? try {

? ? ? ? ? ? System.out.println("Method 2 execute");

? ? ? ? ? ? Thread.sleep(1000);

? ? ? ? } catch (InterruptedException e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ? System.out.println("Method 2 end");

? ? }

? ? ? ? public static void main(String[] args) {

? ? ? ? ? ? final syncTest test = new syncTest();

? ? ? ? ? ? new Thread(new Runnable() {

? ? ? ? ? ? ? ? @Override

? ? ? ? ? ? ? ? public void run() {

? ? ? ? ? ? ? ? ? ? test.method1();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }).start();

? ? ? ? ? ? new Thread(new Runnable() {

? ? ? ? ? ? ? ? @Override

? ? ? ? ? ? ? ? public void run() {

? ? ? ? ? ? ? ? ? ? test.method2();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }).start();

? ? ? ? }

輸出結(jié)果:


分析:當(dāng)線程1還在執(zhí)行時(shí),線程2也執(zhí)行了,所以當(dāng)其他線程來訪問非synchronized修飾的方法時(shí)是可以訪問的

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容