java同步鎖的正確使用

來源:互聯網
上載者:User

標籤:override   null   應用   一個   trace   read   dex   sys   ota   

同步鎖分類對象鎖(this)類鎖(類的位元組碼檔案對象即類名.class)字串鎖(比較特別)應用情境

在多線程下對共用資源的安全操作。

需求:啟動5個線程對共用資源total進行安全操作。

同步鎖在多線程單例模式下的使用

以上三類同步鎖都可以。

package cn.myThread;

public class MyThread implements Runnable {
    private static int total = 10;
    @Override
    public void run() {
        synchronized (this){ //使用this對象鎖
        //synchronized (MyThread.class){ //使用MyThread.class類鎖
        //synchronized (""){
//使用字串鎖
           
System.out.println(Thread.currentThread().getName() + "正在運行");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            total--;
            System.out.println(total);
            System.out.println(Thread.currentThread().getName() + "線程結束");
        }
    }
}

package cn.test;

import cn.myThread.MyThread;

public class TestMyThread {
    public static void main(String[] args){
        MyThread myThread = new MyThread();
        Thread thread = null;
        for (int i = 1 ; i <= 5 ; i++){
            thread = new Thread(myThread,"線程"+i); //開啟5個線程,傳入同一個對象
            thread.start();
        }
    }
}

線程1正在運行

9

線程1線程結束

線程3正在運行

8

線程3線程結束

線程5正在運行

7

線程5線程結束

線程2正在運行

6

線程2線程結束

線程4正在運行

5

線程4線程結束

分析:從運行結果可以看出5個線程串列執行同步鎖裡面的代碼,因為5個線程中的同步鎖對象this指向同一個的對象(同步鎖對象MyThread.class類鎖是同一個對象、同步鎖對象 ”” 字串鎖是同一個對象),所以5個線程會串列執行同步鎖裡面的代碼。

同步鎖在多線程多例模式下的使用錯誤用法

package cn.myThread;

public class MyThread implements Runnable {
    private static int total = 10;
    @Override
    public void run() {
        synchronized (this){//使用this對象鎖
           
System.out.println(Thread.currentThread().getName() + "正在運行");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            total--;
            System.out.println(total);
            System.out.println(Thread.currentThread().getName() + "線程結束");
        }
    }
}

package cn.test;

import cn.myThread.MyThread;

public class TestMyThread {
    public static void main(String[] args){
        Thread thread = null;
        for (int i = 1 ; i <= 5 ; i++){
            thread = new Thread(new MyThread(),"
線程"+i);//開啟5個線程,傳入5個不同對象
            thread.start();
        }
    }
}

線程2正在運行

線程1正在運行

線程3正在運行

線程5正在運行

線程4正在運行

9

7

9

8

線程1線程結束

線程5線程結束

線程2線程結束

線程3線程結束

6

線程4線程結束

分析:從運行結果可以看出5個線程並存執行同步鎖裡面的代碼,因為5個線程中的同步鎖對象this指向5個不同的對象,所以5個線程會同時執行同步鎖裡面的代碼。

正確用法方式一:

package cn.myThread;

public class MyThread implements Runnable {
    private static int total = 10;
    @Override
    public void run() {
       
synchronized (MyThread.class){//使用MyThread.class類鎖
           
System.out.println(Thread.currentThread().getName() + "正在運行");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            total--;
            System.out.println(total);
            System.out.println(Thread.currentThread().getName() + "線程結束");
        }
    }
}

package cn.test;

import cn.myThread.MyThread;

public class TestMyThread {
    public static void main(String[] args){
        Thread thread = null;
        for (int i = 1 ; i <= 5 ; i++){
            thread = new Thread(new MyThread(),"線程"+i); //開啟5個線程,傳入5個不同對象
            thread.start();
        }
    }
}

線程1正在運行

9

線程1線程結束

線程5正在運行

8

線程5線程結束

線程4正在運行

7

線程4線程結束

線程3正在運行

6

線程3線程結束

線程2正在運行

5

線程2線程結束

分析:從運行結果可以看出5個線程串列執行同步鎖裡面的代碼,因為5個線程中的同步鎖對象MyThread.class類鎖是同一個對象,所以5個線程會串列執行同步鎖裡面的代碼。

方式二:

package cn.myThread;

public class MyThread implements Runnable {
    private static int total = 10;
    @Override
    public void run() {
       
synchronized (""){//使用字串鎖
            System.out.println(Thread.currentThread().getName() + "正在運行");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            total--;
            System.out.println(total);
            System.out.println(Thread.currentThread().getName() + "線程結束");
        }
    }
}

package cn.test;

import cn.myThread.MyThread;

public class TestMyThread {
    public static void main(String[] args){
        Thread thread = null;
        for (int i = 1 ; i <= 5 ; i++){
            thread = new Thread(new MyThread(),"線程"+i); //開啟5個線程,傳入5個不同對象
            thread.start();
        }
    }
}

線程1正在運行

9

線程1線程結束

線程4正在運行

8

線程4線程結束

線程5正在運行

7

線程5線程結束

線程3正在運行

6

線程3線程結束

線程2正在運行

5

線程2線程結束

分析:從運行結果可以看出5個線程串列執行同步鎖裡面的代碼,因為5個線程中的同步鎖對象 ”” 字串鎖是同一個對象,所以5個線程會串列執行同步鎖裡面的代碼。

java同步鎖的正確使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.