1.synchronized+類成員方法:(對象鎖)不一樣對象互相不會排斥。spa
a.同一對象調用不一樣synchronized方法:相同對象synchronized方法會互相排斥。線程
public class Test { public synchronized void sy() { for(int i=0; i<5; i++) { System.out.println(Thread.currentThread().getName() + ":" + i); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized void sy2() { for(int i=0; i<5; i++) { System.out.println(Thread.currentThread().getName() + ":" + i); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { final Test t1 = new Test(); final Test t2 = new Test(); new Thread(new Runnable(){ public void run() { t1.sy();//t1對象 }}).start(); new Thread(new Runnable(){ public void run() { t1.sy2();//t1對象 }}).start(); } }
結果以下:code
Thread-0:0
對象
Thread-0:1
get
Thread-0:2
io
Thread-0:3
class
Thread-0:4
方法
Thread-1:0
總結
Thread-1:1
static
Thread-1:2
Thread-1:3
Thread-1:4
b.同一對象調用同一synchronized方法:相同對象會排斥。
public class Test { public synchronized void sy() { for(int i=0; i<5; i++) { System.out.println(Thread.currentThread().getName() + ":" + i); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { final Test t1 = new Test(); final Test t2 = new Test(); new Thread(new Runnable(){ public void run() { t1.sy();//t1對象 }}).start(); new Thread(new Runnable(){ public void run() { t1.sy();//t1對象 }}).start(); } }
結果以下:
Thread-0:0 Thread-0:1 Thread-0:2 Thread-0:3 Thread-0:4 Thread-1:0 Thread-1:1 Thread-1:2 Thread-1:3 Thread-1:4
c.不一樣對象調用不一樣synchronized方法:不一樣對象不會排斥。
public class Test { public synchronized void sy() { for(int i=0; i<5; i++) { System.out.println(Thread.currentThread().getName() + ":" + i); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized void sy2() { for(int i=0; i<5; i++) { System.out.println(Thread.currentThread().getName() + ":" + i); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { final Test t1 = new Test(); final Test t2 = new Test(); new Thread(new Runnable(){ public void run() { t1.sy();//t1對象 }}).start(); new Thread(new Runnable(){ public void run() { t2.sy2();//t2對象 }}).start(); } }
結果以下:
Thread-0:0 Thread-1:0 Thread-0:1 Thread-1:1 Thread-0:2 Thread-1:2 Thread-0:3 Thread-1:3 Thread-0:4 Thread-1:4
d.不一樣對象調用同一synchronized方法:不一樣對象不會排斥。
public class Test { public synchronized void sy() { for(int i=0; i<5; i++) { System.out.println(Thread.currentThread().getName() + ":" + i); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } public synchronized void sy2() { for(int i=0; i<5; i++) { System.out.println(Thread.currentThread().getName() + ":" + i); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { final Test t1 = new Test(); final Test t2 = new Test(); new Thread(new Runnable(){ public void run() { t1.sy();//t1對象 }}).start(); new Thread(new Runnable(){ public void run() { t2.sy());//t2對象 }}).start(); } }
結果以下:
Thread-0:0 Thread-1:0 Thread-1:1 Thread-0:1 Thread-0:2 Thread-1:2 Thread-0:3 Thread-1:3 Thread-1:4 Thread-0:4
2.synchronized+類靜態成員方法:(類鎖)全部對象會排斥。
a.(同一/不一樣)對象調用(同一/不一樣)synchronized方法:都會排斥。
public class Test { public static synchronized void sy() { for(int i=0; i<5; i++) { System.out.println(Thread.currentThread().getName() + ":" + i); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } public static synchronized void sy2() { for(int i=0; i<5; i++) { System.out.println(Thread.currentThread().getName() + ":" + i); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { final Test t1 = new Test(); final Test t2 = new Test(); new Thread(new Runnable(){ public void run() { t1.sy();//t1對象 }}).start(); new Thread(new Runnable(){ public void run() { t1.sy();//t1對象 a //t1.sy2();//t1對象 b //t2.sy();//t2對象 c //t2.sy2();//t2對象 d }}).start(); } }
a,b, c, d四種狀況:
Thread-0:0 Thread-0:1 Thread-0:2 Thread-0:3 Thread-0:4 Thread-1:0 Thread-1:1 Thread-1:2 Thread-1:3 Thread-1:4
總結:
1.多個線程調用同一對象的不一樣synchronized方法,同一時刻只能有一個線程獲得執行,另外一個線程必須等待。
2.多個線程調用不一樣對象的相同synchronized方法,互不影響。
3.多個線程調用(同一/不一樣)對象的(同一/不一樣)static+synchronized方法,同一時刻只能有一個線程獲得執行,另外一個線程必須等待。
關鍵:
靜態方法的鎖爲Class類對象,非靜態方法的鎖爲實例對象。某一時刻,只能有一個線程持有該對象(或者Class對象或者實例對象)的鎖