欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java多線程三種上鎖方式小結(jié)

 更新時(shí)間:2023年12月22日 11:06:16   作者:湖邊看客  
本文主要介紹了java多線程三種上鎖方式小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

以下以兩人共用一個(gè)銀行卡賬戶的方式,同時(shí)取錢(qián),兩條線程同時(shí)執(zhí)行,為了保證線程安全,我們對(duì)關(guān)鍵的部分進(jìn)行加鎖的三種方式

線程類(lèi)

public class DrawThread extends Thread {
    private Account acc;

    public DrawThread(Account acc, String name){
        super(name);
        this.acc=acc;
    }

    @Override
    public  void run(){
        //取錢(qián)  //每個(gè)線程在start的時(shí)候   都會(huì)執(zhí)行  這個(gè)run方法
        acc.drawMoney(10000);
    }
}

賬戶類(lèi)

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Account {
    private  String cardId;
    private  double money;
    //創(chuàng)建一個(gè)鎖對(duì)象
    private final Lock lk =new ReentrantLock(); //手動(dòng)鎖


    public Account() {
    }

    public Account(String cardId, double money) {
        this.cardId = cardId;
        this.money = money;
    }


    //靜態(tài)代碼   用class作為鎖
    public static  void test(){
        synchronized (Account.class){

        }
    }

    //取錢(qián)的方法
    //同步代碼塊上鎖
     /**   public void drawMoney(double money){
            //誰(shuí)來(lái)取錢(qián)?
            String name = Thread.currentThread().getName();
            //判斷余額是否足夠
            synchronized (this){  //this 在這個(gè)過(guò)程中是共同擁有的相同的數(shù)據(jù) 所以能鎖住
                if(this.money>=money){
                    System.out.println(name+"取錢(qián)"+money+"成功!");
                    this.money -= money;
                    System.out.println(name+"取錢(qián)后,剩余"+this.money);
                }else {
                    System.out.println(name+"余額不足");
                }
            }
        }
    */

    //取錢(qián)方法
    //同步方法上鎖
    /** public synchronized void drawMoney(double money){
         //誰(shuí)來(lái)取錢(qián)?
         String name = Thread.currentThread().getName();
         //判斷余額是否足夠
             if(this.money>=money){
                 System.out.println(name+"取錢(qián)"+money+"成功!");
                 this.money -= money;
                 System.out.println(name+"取錢(qián)后,剩余"+this.money);
             }else {
                 System.out.println(name+"余額不足");
             }

     }
    */

    //取錢(qián)的方法
    //手動(dòng)鎖
    public synchronized void drawMoney(double money){
        //誰(shuí)來(lái)取錢(qián)?
        String name = Thread.currentThread().getName();
        lk.lock(); //加鎖
        //判斷余額是否足夠
        if(this.money>=money){
            System.out.println(name+"取錢(qián)"+money+"成功!");
            this.money -= money;
            System.out.println(name+"取錢(qián)后,剩余"+this.money);
        }else {
            System.out.println(name+"余額不足");
        }
        lk.unlock(); //解鎖
    }




    public String getCardId() {
        return cardId;
    }

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

}

執(zhí)行

public class ThreadTest {

    public static void main(String[] args) {
        //創(chuàng)建一個(gè)賬戶
        Account acc = new Account("ic000",10000);
        //創(chuàng)建兩個(gè)線程分別代表小明和小紅  同時(shí)  在這個(gè)個(gè)賬戶中取錢(qián)


        new DrawThread(acc,"小明").start();

        new DrawThread(acc,"小紅").start();


        Account acc2 = new Account("ic002",10000);
        //創(chuàng)建兩個(gè)線程分別代表小明和小紅  同時(shí)  在這個(gè)個(gè)賬戶中取錢(qián)

        new DrawThread(acc2,"小黑").start();

        new DrawThread(acc2,"小白").start();

    }

}

效果: 

到此這篇關(guān)于java多線程三種上鎖方式小結(jié)的文章就介紹到這了,更多相關(guān)java多線程上鎖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

最新評(píng)論