java多線程鎖同步控制

1、使用關鍵字:synchronized java

一、synchronized能夠修飾方法,(其實也是鎖的是當前方法的對象),也能夠修飾代碼塊,修飾代碼也須要傳入一個對象。 synchronized(object){}程序員

二、兩種方式都是獲取的是synchronized修飾對象的monitor鎖!這纔是真正的鎖!ide

三、synchronized所修飾的鎖不能夠爲null!線程

public class Main{
    private static int commonTag = 0;
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    add(Thread.currentThread().getId());

                }
            }
        };
        Thread t1 =  new Thread(runnable);
        Thread t2 =  new Thread(runnable);
        Thread t3 =  new Thread(runnable);
        Thread t4 =  new Thread(runnable);
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }

   public static synchronized void add(long ID){
        System.out.println("當前線程ID:"+ID+"執行結果="+commonTag++);
    }

//   public static  void add(long ID){
//        synchronized(object){  //也須要傳入一個對象
//            System.out.println("當前線程ID:"+ID+"執行結果="+commonTag++);
//
//        }
//    }
}
當前線程ID:10執行結果=0
當前線程ID:11執行結果=1
當前線程ID:12執行結果=2
當前線程ID:13執行結果=3
當前線程ID:10執行結果=4
當前線程ID:12執行結果=5
當前線程ID:13執行結果=6
當前線程ID:11執行結果=7
當前線程ID:10執行結果=8
當前線程ID:13執行結果=9
當前線程ID:12執行結果=10
當前線程ID:11執行結果=11
當前線程ID:10執行結果=12
當前線程ID:13執行結果=13
當前線程ID:12執行結果=14
當前線程ID:11執行結果=15

若是不用則結果以下:code

當前線程ID:12執行結果=3
當前線程ID:10執行結果=1
當前線程ID:13執行結果=2
當前線程ID:11執行結果=0
當前線程ID:12執行結果=5
當前線程ID:11執行結果=7
當前線程ID:13執行結果=6
當前線程ID:10執行結果=4
當前線程ID:11執行結果=8
當前線程ID:10執行結果=9
當前線程ID:13執行結果=9
當前線程ID:12執行結果=8

2、死鎖的緣由和如何避免!對象

對於程序員須要關注兩個方向:一個是內存不足引發的死鎖,另外一個是交叉鎖,各持對方的排它鎖致使的死鎖,誰都不願放!內存

public class Main{
    private static int commonTag = 0;
    private static Object object1 = new Object();
    private static Object object2 = new Object();
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    add(Thread.currentThread().getId());

                }
            }
        };
        Runnable runnable2 = new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    add2(Thread.currentThread().getId());

                }
            }
        };

        Thread t1 =  new Thread(runnable);
        Thread t2 =  new Thread(runnable);
        Thread t3 =  new Thread(runnable2);
        Thread t4 =  new Thread(runnable2);
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }

   public static  void add(long ID){
        synchronized(object1){
            synchronized(object2){
                System.out.println("當前線程ID:"+ID+"執行結果="+commonTag++);

            }
        }
    }
    public static  void add2(long ID){
        synchronized(object2){
            synchronized(object1){
                System.out.println("當前線程ID:"+ID+"執行結果="+commonTag++);

            }
        }
    }

}

3、LOCK(java.util.concurrent.locks)get

一、讀寫鎖?it

二、自旋鎖io

import java.util.concurrent.locks.ReentrantLock;
public class Main{
    private static int commonTag = 0;
    private  static ReentrantLock lock = new ReentrantLock();

    private static   Object object = new Object();
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    add(Thread.currentThread().getId());

                }
            }
        };
        Thread t1 =  new Thread(runnable);
        Thread t2 =  new Thread(runnable);
        Thread t3 =  new Thread(runnable);
        Thread t4 =  new Thread(runnable);
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
   public static  void add(long ID){
        try{
            lock.lock();
            System.out.println("當前線程ID:"+ID+"執行結果="+commonTag++);
        }finally {
            lock.unlock();
        }

    }
}
相關文章
相關標籤/搜索