1. 程式人生 > 其它 >|NO.Z.00103|——————————|BigDataEnd|——|Java&多執行緒.V15|------------------------------------------------|Java.v15|同步程式碼塊|實現執行緒|同步方式|

|NO.Z.00103|——————————|BigDataEnd|——|Java&多執行緒.V15|------------------------------------------------|Java.v15|同步程式碼塊|實現執行緒|同步方式|



[BigDataJava:Java&多執行緒.V15]                                                                                 [BigDataJava.核心類庫] [|章節三|多執行緒|同步程式碼塊實現執行緒同步方式二|同步方法實現執行緒同步的方式一|]








一、同步帶嗎塊實現同步方式二
package com.yanqi.task18;

public class AccountThreadTest extends Thread {
    private int balance; // 用於描述賬戶的餘額
    private static Demo dm = new Demo(); // 隸屬於類層級,所有物件共享同一個

    public AccountThreadTest() {
    }

    public AccountThreadTest(int balance) {
        this.balance = balance;
    }

    public int getBalance() {
        return balance;
    }

    public void setBalance(int balance) {
        this.balance = balance;
    }

    @Override
    public /*static*/ /*synchronized*/ void run() {
        /*System.out.println("執行緒" + Thread.currentThread().getName() + "已啟動...");
        //synchronized (dm) { // ok
            //synchronized (new Demo()) { // 鎖不住  要求必須是同一個物件
            // 1.模擬從後臺查詢賬戶餘額的過程
            int temp = getBalance(); // temp = 1000  temp = 1000
            // 2.模擬取款200元的過程
            if (temp >= 200) {
                System.out.println("正在出鈔,請稍後...");
                temp -= 200;  // temp = 800   temp = 800
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("請取走您的鈔票!");
            } else {
                System.out.println("餘額不足,請核對您的賬戶餘額!");
            }
            // 3.模擬將最新的賬戶餘額寫入到後臺
            setBalance(temp); // balance = 800  balance = 800
        //}*/
        test();
    }

    public /*synchronized*/ static void test() {
        synchronized (AccountThreadTest.class) { // 該型別對應的Class物件,由於型別是固定的,因此Class物件也是唯一的,因此可以實現同步
            System.out.println("執行緒" + Thread.currentThread().getName() + "已啟動...");
            //synchronized (dm) { // ok
            //synchronized (new Demo()) { // 鎖不住  要求必須是同一個物件
            // 1.模擬從後臺查詢賬戶餘額的過程
            int temp = 1000; //getBalance(); // temp = 1000  temp = 1000
            // 2.模擬取款200元的過程
            if (temp >= 200) {
                System.out.println("正在出鈔,請稍後...");
                temp -= 200;  // temp = 800   temp = 800
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("請取走您的鈔票!");
            } else {
                System.out.println("餘額不足,請核對您的賬戶餘額!");
            }
            // 3.模擬將最新的賬戶餘額寫入到後臺
            //setBalance(temp); // balance = 800  balance = 800
        }
    }

    public static void main(String[] args) {

        AccountThreadTest att1 = new AccountThreadTest(1000);
        att1.start();

        AccountThreadTest att2 = new AccountThreadTest(1000);
        att2.start();

        System.out.println("主執行緒開始等待...");
        try {
            att1.join();
            //t2.start(); // 也就是等待執行緒一取款操作結束後再啟動執行緒二
            att2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("最終的賬戶餘額為:" + att1.getBalance()); // 800

    }

}
二、編譯列印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=51182:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task18.AccountThreadTest
主執行緒開始等待...
執行緒Thread-0已啟動...
正在出鈔,請稍後...
請取走您的鈔票!
執行緒Thread-1已啟動...
正在出鈔,請稍後...
請取走您的鈔票!
最終的賬戶餘額為:1000

Process finished with exit code 0








===============================END===============================


Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart                                                                                                                                                    ——W.S.Landor



來自為知筆記(Wiz)