Synchronized詳解

1、使用零長度的byte[]對象做爲鎖數組

class Foo implements Runnablethis

{spa

        private byte[] lock = new byte[0]; // 特殊的instance變量對象

        Public void methodA()
        {get

           synchronized(lock) { //… }io

        }編譯

        //…..class

}test

注:零長度的byte數組對象建立起來將比任何對象都經濟――查看編譯後的字節碼:生成零長度的byte[]對象只需3條操做碼,而Object lock變量

= new Object()則須要7行操做碼。

 

2、synchronized做用範圍

一、只對對象生效,不對類生效

public synchronized void test1(){
    while (i<=5){
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+":print-"+i);
        i++;
    }
public  void test(){
    synchronized (this){
        while (i<=5){
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+":print-"+i);
            i++;
        }
    }
}
private final int[] lock = new int[0];
public void test2(){
    {
        synchronized (lock){
            while (i<=5){
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+":print-"+i);
                i++;
            }
        }
    }
}b、

 

二、對類生效

靜態變量

private static final int[] lock4 = new int[0];
public void test4(){
    {
        int j = 0;
        synchronized (lock4){
            while (j<=5){
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+":print-"+j);
                j++;
            }
        }
    }
}

類名做爲鎖

public void test5(){
    {
        int j = 0;
        synchronized (SynchronizedClass2.class){
            while (j<=5){
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+":print-"+j);
                j++;
            }
        }
    }
}

三、同一對象中多個對象鎖不會互相影響

private final int[] lock2 = new int[0];
public void test2(){
    {
        int j = 0;
        synchronized (lock2){
            while (j<=5){
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+":print-"+j);
                j++;
            }
        }
    }
}

private final int[] lock3 = new int[0];
public void test3(){
    {
        synchronized (lock3){
            while (i<=5){
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+":print-"+i);
                i++;
            }
        }
    }
}

四、靜態方法加鎖不會影響同一類、同一對象的其餘方法,隻影響同一類的自身方法;非靜態方法加鎖影響同一對象中的全部加鎖方法,不影響同一類其餘對象的加鎖方法。

相關文章
相關標籤/搜索