Java中解決多執行緒資料安全問題

季沐測試筆記發表於2021-08-16

同步程式碼塊

基本語句

synchronized (任意物件) {
操作共享程式碼
}

程式碼示例


public class SellTicket implements Runnable {
    private int tickets = 100;
    private Object object = new Object();


    @Override
    public void run() {
        while (true) {
            synchronized (object) {
                if (tickets > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets + "張票");
                    tickets--;
                }
            }
        }
    }


    public static void main(String[] args) {

        SellTicket sellTicket = new SellTicket();

        Thread thread1 = new Thread(sellTicket, "視窗1");
        Thread thread2 = new Thread(sellTicket, "視窗2");
        Thread thread3 = new Thread(sellTicket, "視窗3");
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

優缺點:

  • 解決了多執行緒的資料安全問題
  • 多執行緒時,每個執行緒都會判斷同步上的鎖,耗費資源,降低了程式的執行效率

同步方法

同步方法:將synchronized關鍵字加到方法上

  • 格式: 修飾符 synchronized 返回值型別 方法名(){ }
  • 同步方法的鎖物件是this

同步靜態方法,就是把synchronized關鍵字加到靜態方法上

  • 格式: 修飾符 static synchronized 返回值型別 方法名(){ }
  • 同步靜態方法的鎖物件是 類名.class

程式碼示例

public class SellTicket implements Runnable {
//    private int tickets = 100;
    private static int tickets = 100;
    private Object object = new Object();
    private int x = 0;


    @Override
    public void run() {
        while (true) {
            if (x % 2 == 0) {
//                synchronized (object) {
//                synchronized (this) {
                synchronized (SellTicket.class) {
                    if (tickets > 0) {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets + "張票");
                        tickets--;
                    }
                }
            } else {
//                synchronized (object) {
//                    if (tickets > 0) {
//                        try {
//                            Thread.sleep(100);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                        System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets + "張票");
//                        tickets--;
//                    }
//                }
                sellTicket();
            }
            x++;
        }
    }

//    private void sellTicket(){
//        synchronized (object) {
//            if (tickets > 0) {
//                try {
//                    Thread.sleep(100);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets + "張票");
//                tickets--;
//            }
//        }
//    }

//    private synchronized void sellTicket(){
//            if (tickets > 0) {
//                try {
//                    Thread.sleep(100);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets + "張票");
//                tickets--;
//            }
    private static synchronized void sellTicket(){
        if (tickets > 0) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets + "張票");
            tickets--;
        }

    }



    public static void main(String[] args) {

        SellTicket sellTicket = new SellTicket();

        Thread thread1 = new Thread(sellTicket, "視窗1");
        Thread thread2 = new Thread(sellTicket, "視窗2");
        Thread thread3 = new Thread(sellTicket, "視窗3");
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

lock鎖

lock實現提供比使用synchronized方法和語句可獲得更廣泛的操作

  • void lock()獲得鎖
  • void unlock()釋放

lock是介面不能直接例項化,採用實現類例項化ReentrantLock

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



public class SellTicket implements Runnable {
    private int tickets = 100;
    private Object object = new Object();
    private Lock lock = new ReentrantLock();


    @Override
    public void run() {
        while (true) {
            try {
                lock.lock();
                if (tickets > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets + "張票");
                    tickets--;
                }
            } finally {
                lock.unlock();

            }
        }

    }

    public static void main(String[] args) {

        SellTicket sellTicket = new SellTicket();

        Thread thread1 = new Thread(sellTicket, "視窗1");
        Thread thread2 = new Thread(sellTicket, "視窗2");
        Thread thread3 = new Thread(sellTicket, "視窗3");
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

相關文章