package j2se.thread.test; /*** * synchronized(class)很特別,它會讓另外一個線程在任何須要獲取class作爲monitor的地方等待. * class與this作爲不一樣的監視器能夠同時使用,不存在一個線程獲取了class,另外一個線程就不能獲取該class的一切實例. 根據下面的代碼自行修改,分別驗證下面的幾種狀況: synchronized(class) synchronized(this) ->線程各自獲取monitor,不會有等待. synchronized(this) synchronized(this) ->若是不一樣線程監視同一個實例對象,就會等待,若是不一樣的實例,不會等待. synchronized(class) synchronized(class) ->若是不一樣線程監視同一個實例或者不一樣的實例對象,都會等待. */ /** * @author liwei 測試synchronized(this)與synchronized(class) */ public class TestSynchronied8 { private static byte[] lockStatic = new byte[0]; // 特殊的instance變量 private byte[] lock = new byte[0]; // 特殊的instance變量 public synchronized void m4t5() { System.out.println(this); int i = 50; while( i-- > 0) { System.out.println(Thread.currentThread().getName() + " : " + i); try { Thread.sleep(100); } catch (InterruptedException ie) { } Thread.yield(); } } public void m4t0() { synchronized(this) { System.out.println(this); int i = 50; while( i-- > 0) { System.out.println(Thread.currentThread().getName() + " : " + i); try { Thread.sleep(100); } catch (InterruptedException ie) { } Thread.yield(); } } } public void m4t1() { synchronized(lock) { System.out.println(this); int i = 50; while( i-- > 0) { System.out.println(Thread.currentThread().getName() + " : " + i); try { Thread.sleep(100); } catch (InterruptedException ie) { } Thread.yield(); } } } /** * synchronized(class) synchronized(class) ->若是不一樣線程監視同一個實例或者不一樣的實例對象,都會等待. */ public static synchronized void m4t2() { int i = 50; while( i-- > 0) { System.out.println(Thread.currentThread().getName() + " : " + i); try { Thread.sleep(100); } catch (InterruptedException ie) { } Thread.yield(); } } public static void m4t3() { synchronized(TestSynchronied8.class){ int i = 50; while( i-- > 0) { System.out.println(Thread.currentThread().getName() + " : " + i); try { Thread.sleep(100); } catch (InterruptedException ie) { } Thread.yield(); } } } public static void m4t4() { synchronized(lockStatic){ int i = 50; while( i-- > 0) { System.out.println(Thread.currentThread().getName() + " : " + i); try { Thread.sleep(100); } catch (InterruptedException ie) { } Thread.yield(); } } } /** synchronized(this) synchronized(this) ->若是不一樣線程監視不一樣的實例,不會等待. */ public static void testObjsyn1(){ final TestSynchronied8 myt2 = new TestSynchronied8(); final TestSynchronied8 myt1 = new TestSynchronied8(); try { System.out.println("測試兩個不一樣的對象上,運行同一個synchronized(this)代碼塊-------------------------------------"); Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } Thread t1 = new Thread( new Runnable() { public void run() { myt1.m4t0(); } }, "t1" ); Thread t2 = new Thread( new Runnable() { public void run() { myt2.m4t0(); } }, "t2" ); t1.start(); t2.start(); } /** synchronized(this) synchronized(this) ->若是不一樣線程監視同一個實例,會等待. */ public static void testObjsyn2(){ final TestSynchronied8 myt1 = new TestSynchronied8(); try { System.out.println("測試一個對象上,運行同一個synchronized(this)代碼塊-------------------------------------"); Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } Thread t3 = new Thread( new Runnable() { public void run() { myt1.m4t0(); } }, "t3" ); Thread t4 = new Thread( new Runnable() { public void run() { myt1.m4t0(); } }, "t4" ); t3.start(); t4.start(); } /** synchronized(this) synchronized(this) ->若是不一樣線程監視同一個實例,會等待. */ public static void testObjsyn3(){ final TestSynchronied8 myt1 = new TestSynchronied8(); try { System.out.println("測試一個對象上,運行同一個synchronized(obj)代碼塊-------------------------------------"); Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } Thread t5 = new Thread( new Runnable() { public void run() { myt1.m4t1(); } }, "t5" ); Thread t6 = new Thread( new Runnable() { public void run() { myt1.m4t1(); } }, "t6" ); t5.start(); t6.start(); } /** synchronized(this) synchronized(this) ->若是不一樣線程監視同一個實例,會等待. */ public static void testObjsyn4(){ final TestSynchronied8 myt1 = new TestSynchronied8(); try { System.out.println("測試一個對象上,運行同一個synchronized方法-------------------------------------"); Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } Thread t7 = new Thread( new Runnable() { public void run() { myt1.m4t5(); } }, "t7" ); Thread t8 = new Thread( new Runnable() { public void run() { myt1.m4t5(); } }, "t8" ); t7.start(); t8.start(); } /** synchronized(this) synchronized(this) ->若是不一樣線程監視不一樣的實例,不會等待. */ public static void testObjsyn5(){ final TestSynchronied8 myt1 = new TestSynchronied8(); final TestSynchronied8 myt2 = new TestSynchronied8(); try { System.out.println("測試兩個不一樣對象上,運行同一個synchronized方法-------------------------------------"); Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } Thread t9 = new Thread( new Runnable() { public void run() { myt1.m4t5(); } }, "t9" ); Thread t10 = new Thread( new Runnable() { public void run() { myt2.m4t5(); } }, "t10" ); t9.start(); t10.start(); } /** synchronized(this) synchronized方法 ->若是不一樣線程監視同一實例,一個是syschronized(this),一個是同步方法,會等待. */ public static void testObjsyn6(){ final TestSynchronied8 myt1 = new TestSynchronied8(); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } Thread t9 = new Thread( new Runnable() { public void run() { myt1.m4t0(); } }, "t9" ); Thread t10 = new Thread( new Runnable() { public void run() { myt1.m4t5(); } }, "t10" ); t9.start(); t10.start(); } /** synchronized(lock) synchronized方法 ->若是不一樣線程監視同一實例,一個是syschronized(lock),一個是同步方法,不會等待. */ public static void testObjsyn7(){ final TestSynchronied8 myt1 = new TestSynchronied8(); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } Thread t9 = new Thread( new Runnable() { public void run() { myt1.m4t1(); } }, "t9" ); Thread t10 = new Thread( new Runnable() { public void run() { myt1.m4t5(); } }, "t10" ); t9.start(); t10.start(); } /** synchronized(lock) synchronized方法 ->若是不一樣線程監視同一實例,一個是syschronized(lock),一個是syschronized(this),不會等待. */ public static void testObjsyn71(){ final TestSynchronied8 myt1 = new TestSynchronied8(); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } Thread t9 = new Thread( new Runnable() { public void run() { myt1.m4t0(); } }, "t9" ); Thread t10 = new Thread( new Runnable() { public void run() { myt1.m4t1(); } }, "t10" ); t9.start(); t10.start(); } /** * synchronized(class) synchronized(class) ->若是不一樣線程監視不一樣的實例對象,會等待. */ public static void testObjsyn8(){ final TestSynchronied8 myt1 = new TestSynchronied8(); final TestSynchronied8 myt2 = new TestSynchronied8(); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } Thread t2 = new Thread( new Runnable() { public void run() { myt2.m4t2(); } }, "t2" ); Thread t4 = new Thread( new Runnable() { public void run() { myt1.m4t2(); } }, "t4" ); t2.start(); t4.start(); } /** * synchronized(class) synchronized(class) ->若是不一樣線程監視同一的實例對象,會等待. */ public static void testObjsyn9(){ final TestSynchronied8 myt1 = new TestSynchronied8(); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } Thread t2 = new Thread( new Runnable() { public void run() { myt1.m4t2(); } }, "t2" ); Thread t4 = new Thread( new Runnable() { public void run() { myt1.m4t2(); } }, "t4" ); t2.start(); t4.start(); } /** * synchronized(class) synchronized(this) ->線程各自獲取monitor,不會有等待. */ public static void testObjsyn10(){ final TestSynchronied8 myt1 = new TestSynchronied8(); final TestSynchronied8 myt2 = new TestSynchronied8(); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } Thread t7 = new Thread( new Runnable() { public void run() { myt2.m4t0(); } }, "t7" ); Thread t8 = new Thread( new Runnable() { public void run() { myt1.m4t3(); } }, "t8" ); t7.start(); t8.start(); } /** * synchronized(class) synchronized(this) ->線程各自獲取monitor,不會有等待. */ public static void testObjsyn11(){ final TestSynchronied8 myt1 = new TestSynchronied8(); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } Thread t7 = new Thread( new Runnable() { public void run() { myt1.m4t0(); } }, "t7" ); Thread t8 = new Thread( new Runnable() { public void run() { myt1.m4t3(); } }, "t8" ); t7.start(); t8.start(); } /** * synchronized(class) synchronized(lock) ->線程各自獲取monitor,不會有等待. */ public static void testObjsyn12(){ final TestSynchronied8 myt1 = new TestSynchronied8(); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } Thread t7 = new Thread( new Runnable() { public void run() { myt1.m4t1(); } }, "t7" ); Thread t8 = new Thread( new Runnable() { public void run() { myt1.m4t3(); } }, "t8" ); t7.start(); t8.start(); } /** * synchronized(class) synchronized(lockStatic) ->線程各自獲取monitor,不會有等待. */ public static void testObjsyn13(){ final TestSynchronied8 myt1 = new TestSynchronied8(); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } Thread t7 = new Thread( new Runnable() { public void run() { myt1.m4t4(); } }, "t7" ); Thread t8 = new Thread( new Runnable() { public void run() { myt1.m4t3(); } }, "t8" ); t7.start(); t8.start(); } /** * synchronized方法 synchronized(lockStatic) ->線程各自獲取monitor,不會有等待. */ public static void testObjsyn14(){ final TestSynchronied8 myt1 = new TestSynchronied8(); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } Thread t7 = new Thread( new Runnable() { public void run() { myt1.m4t4(); } }, "t7" ); Thread t8 = new Thread( new Runnable() { public void run() { myt1.m4t2(); } }, "t8" ); t7.start(); t8.start(); } /** * synchronized方法 synchronized(lockStatic) ->線程各自獲取monitor,不會有等待. */ public static void testObjsyn15(){ final TestSynchronied8 myt1 = new TestSynchronied8(); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } Thread t7 = new Thread( new Runnable() { public void run() { myt1.m4t3(); } }, "t7" ); Thread t8 = new Thread( new Runnable() { public void run() { myt1.m4t2(); } }, "t8" ); t7.start(); t8.start(); } /** * synchronized方法 synchronized(lockStatic) ->線程各自獲取monitor,不會有等待. */ public static void testObjsyn16(){ final TestSynchronied8 myt1 = new TestSynchronied8(); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } Thread t7 = new Thread( new Runnable() { public void run() { myt1.m4t1(); } }, "t7" ); Thread t8 = new Thread( new Runnable() { public void run() { myt1.m4t4(); } }, "t8" ); t7.start(); t8.start(); } /** * synchronized(class)和static synchronized 方法所獲取的鎖是同樣的 * synchronized(class)方法和靜態的方法中的 synchronized(lockStatic)代碼塊獲取的鎖是不同的 * 非靜態的方法中的synchronized(this)代碼塊和非靜態的 synchronized 方法所獲取的鎖是同樣的 * (靜態和非靜態) synchronized(this)和(靜態和非靜態)的 synchronized(lock)代碼塊獲取的鎖是不同的 * * 總的來講synchronized(class)和static synchronized 方法獲取的是類鎖 * 非靜態的方法中的synchronized(this)代碼塊和非靜態的 synchronized 方法所獲取的鎖是類的實例的對象鎖 * synchronized(lockStatic)獲取的是靜態屬性的鎖 * synchronized(lock) 獲取的是非靜態屬性的鎖 * 若是獲取的鎖是同樣的,代碼就會同步 ;鎖不同就不會同步 */ public static void main(String[] args) { //對於非靜態的方法,同步方法和 synchronized(this) 獲取的是實例對象鎖 //對於非靜態的方法,同步方法和 synchronized(lock) 獲取的鎖的lock // testObjsyn1();//若是不一樣線程監視不一樣的實例,不會等待. synchronized(this) // testObjsyn2();//若是不一樣線程監視同一個實例,會等待. synchronized(this) // testObjsyn3();//若是不一樣線程監視同一個實例,會等待. synchronized(lock) // testObjsyn4();//若是不一樣線程監視同一個實例,會等待. synchronized 方法 // testObjsyn5();//若是不一樣線程監視不一樣的實例,不會等待. synchronized 方法 // testObjsyn6();//若是不一樣線程監視同一實例,一個是syschronized(this),一個是同步方法,會等待 // testObjsyn7(); //若是不一樣線程監視同一實例,一個是syschronized(lock),一個是同步方法,不會等待,鎖定的對象不同的 // testObjsyn71(); //若是不一樣線程監視同一實例,一個是syschronized(lock),一個是syschronized(this),不會等待,鎖定的對象不同的 /** * synchronized(class) synchronized(class) ->若是不一樣線程監視同一個實例或者不一樣的實例對象,都會等待. */ // testObjsyn8();//若是不一樣線程監視不一樣的實例對象,會等待.synchronized(class) // testObjsyn9();//若是不一樣線程監視同一的實例對象,會等待.synchronized(class) // testObjsyn10();//若是不一樣線程監視不一樣的實例對象,不會等待.synchronized(class),synchronized(this) // testObjsyn11();//若是不一樣線程監視同一的實例對象,不會等待.synchronized(class),synchronized(this) // testObjsyn12();//若是不一樣線程監視同一的實例對象,不會等待.synchronized(class),synchronized(lock) // testObjsyn13();//若是不一樣線程監視同一的實例對象,不會等待.synchronized(class),synchronized(lockStatic) // testObjsyn14();//若是不一樣線程監視同一的實例對象,不會等待.synchronized()方法,synchronized(lockStatic) // testObjsyn15();//若是不一樣線程監視同一的實例對象,會等待.synchronized()方法,synchronized(class) // testObjsyn16();//若是不一樣線程監視同一的實例對象,不會等待.synchronized(lock)方法,synchronized(lockStatic) } }