java synchronized修飾普通方法,修飾靜態方法,修飾代碼塊,修飾線程run方法 比較

 

synchronized用於多線程設計,有了synchronized關鍵字,多線程程序的運行結果將變得能夠控制。synchronized關鍵字用於保護共享數據。多線程

synchronized實現同步的機制:synchronized依靠"鎖"機制進行多線程同步,"鎖"有2種,一種是對象鎖,一種是類鎖ide

  • 1.依靠對象鎖鎖定

初始化一個對象時,自動有一個對象鎖。synchronized {普通方法}依靠對象鎖工做,多線程訪問synchronized方法,一旦某個進程搶得鎖以後,其餘的進程只有排隊對待。函數


synchronized {普通方法}依靠對象鎖工做,多線程訪問synchronized方法,一旦某個進程搶得鎖以後,其餘的進程只有排隊對待。
 
synchronized void method{}功能上,等效於
void method{
   synchronized(this) {
    ...
   }
}
經過代碼看比較清楚:
public class TestSynchronized {
    public synchronized void method1() throws InterruptedException {
        System.out.println("method1 begin at:" + System.currentTimeMillis());
        Thread.sleep(6000);
        System.out.println("method1 end at:" + System.currentTimeMillis());
    }
    public synchronized void method2() throws InterruptedException {
        while(true) {
            System.out.println("method2 running");
            Thread.sleep(200);
        }
    }
    static TestSychronized instance = new TestSychronized();
    public static void main(String[] args) {
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    instance.method1();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for(int i=1; i<4; i++) {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("Thread1 still alive");
                }                    
            }
        });
        
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    instance.method2();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        
        thread1.start();
        thread2.start();    
        
    }
}

運行結果:thread2一直等到thread1中的method1執行完了以後才執行method2,說明method1和method2互斥this

method1 begin at:1381584063557
method1 end at:1381584069557
method2 running
method2 running
Thread1 still alive
method2 running
Thread1 still alive
method2 running
Thread1 still alive
method2 running
method2 running
method2 running
method2 running
method2 running
method2 running
method2 running
method2 running

 

 
synchronized {修飾代碼塊}的做用不只於此,synchronized void method{}整個函數加上synchronized塊,效率並很差。在函數內部,可能咱們須要同步的只是小部分共享數據,其餘數據,能夠自由訪問,這時候咱們能夠 用 synchronized(表達式){//語句}更加精確的控制
  • 2.synchronized {static方法}此代碼塊等效於
void method{
   synchronized(Obl.class)
   }
}
使用該類的類對象的鎖定去作線程的共享互斥.
package com.free4lab.lol;

public class TestSychronized {
    public synchronized static void method1() throws InterruptedException {
        System.out.println("method1 begin at:" + System.currentTimeMillis());
        Thread.sleep(6000);
        System.out.println("method1 end at:" + System.currentTimeMillis());
    }
    public synchronized static void method2() throws InterruptedException {
        while(true) {
            System.out.println("method2 running");
            Thread.sleep(200);
        }
    }
    static TestSychronized instance1 = new TestSychronized();
    static TestSychronized instance2 = new TestSychronized();
    public static void main(String[] args) {
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    instance1.method1();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for(int i=1; i<4; i++) {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("Thread1 still alive");
                }                    
            }
        });
        
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    instance2.method2();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        
        thread1.start();
        thread2.start();    
        
    }
}

輸出效果也是method1和method2互斥spa

 
  • 3.synchronized {run方法}run方法的鎖定.
這個舉例比較好說。
package com.free4lab.lol;

public class TestSychronized {
    static TestSychronized instance = new TestSychronized();
    public static void main(String[] args) {
        Thread thread1 = new Thread(new Runnable() {
            @Override
                public synchronized void run() {
                    
                    for(int i=1; i<4; i++) {
                        try {
                            Thread.sleep(200);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("Thread1 still alive, " + i);
                    }                    
                }
        });
        new Thread(thread1).start();
        new Thread(thread1).start();
    }
}

若是加了synchronized當前線程取完全部數據後,纔會釋放鎖,輸出結果是有序的:線程

Thread1 still alive, 1
Thread1 still alive, 2
Thread1 still alive, 3
Thread1 still alive, 1
Thread1 still alive, 2
Thread1 still alive, 3
相關文章
相關標籤/搜索