上代碼,可直接跑,思路是用一個對象,寫了同步對象方法,同步靜態方法,以及synchronized修飾資源,經過多線程打印出信息,分析出不一樣:安全
測試對象多線程
SynchronizedObjide
package shiroexample.thread; /** * shiroexample.thread * functional describe: * * @author DR.YangLong [410357434@163.com] * @version 1.0 2015/4/17 */ public class SynchronizedObj { private int n=1; private Integer blockObj=1; public SynchronizedObj() { } public int getN() { return n++; } public synchronized int getNs(){ return n++; } public void setN(int n) { this.n = n; } public synchronized void testWait(){ System.out.println("進入testWait!"); try { Thread.sleep(10000); }catch (Exception e){ e.printStackTrace(); }finally { System.out.println("出testWait!"); } } public synchronized void testBlockWait(){ System.out.println("運行testBlockWait!"); } public synchronized static void testStaticBlockWait(){ System.out.println("運行testStaticBlockWait!"); } public synchronized static void testStaticWait(){ System.out.println("進入testStaticWait!"); try { Thread.sleep(10000); }catch (Exception e){ e.printStackTrace(); }finally { System.out.println("出testStaticWait!"); } } public void plus(){ synchronized (blockObj){ System.out.println("進入synchronized加在資源上......"); try { Thread.sleep(10000); }catch (Exception e){ e.printStackTrace(); }finally { System.out.println("出synchronized加在資源上......"); } } } public void plus_(){ synchronized (this){ System.out.println("進入synchronized加在this上......"); try { Thread.sleep(10000); }catch (Exception e){ e.printStackTrace(); }finally { System.out.println("出synchronized加在this上......"); } } } }
線程不安全測試用例SynTest測試
package shiroexample.thread; /** * shiroexample.thread * functional describe: * * @author DR.YangLong [410357434@163.com] * @version 1.0 2015/4/17 */ public class SynTest { public static void main(String[] args) { SynchronizedObj obj = new SynchronizedObj(); normalTest(obj); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } synchronizedTest(obj); } public static void normalTest(final SynchronizedObj obj){ /*********************沒有同步,會有機率相同,特別是起始時**************************/ Thread one = new Thread( new Runnable() { @Override public void run() { int i = 0; while (i++ < 100) System.out.print("one:" + obj.getN() + " "); } } ); Thread two = new Thread( new Runnable() { @Override public void run() { int i = 0; while (i++ < 100) System.out.print("two:" + obj.getN() + " "); } } ); Thread three = new Thread( new Runnable() { @Override public void run() { int i = 0; while (i++ < 100) System.out.print("three:" + obj.getN() + " "); } } ); one.start(); two.start(); three.start(); } public static void synchronizedTest(final SynchronizedObj obj){ /***************************同步,對象鎖,只能有一個線程訪問對象內的Synchronized修飾的方法******************************/ Thread s1=new Thread(new Runnable() { @Override public void run() { int i=0; while ( i++<100)System.out.print("s1:"+obj.getNs()+" "); } }); Thread s2=new Thread(new Runnable() { @Override public void run() { int i=0; while ( i++<100)System.out.print("s2:"+obj.getNs()+" "); } }); Thread s3=new Thread(new Runnable() { @Override public void run() { int i=0; while ( i++<100)System.out.print("s3:"+obj.getNs()+" "); } }); s1.start(); s2.start(); s3.start(); } }
synchronized關鍵字不用位置測試用例SynLockTestthis
package shiroexample.thread; /** * shiroexample.thread * functional describe: * * @author DR.YangLong [410357434@163.com] * @version 1.0 2015/4/20 */ public class SynLockTest { public static void main(String[] args){ //*Wait方法會休眠,*BlockWait方法直接打印一句話,用以驗證阻塞 SynLockTest synLockTest =new SynLockTest(); /*********同對象中實例方法相互競爭**********/ //synLockTest.testObjClock(new SynchronizedObj()); /*********相同類中靜態方法之間相互競爭,至關於類鎖**********/ //synLockTest.testClassClock(); /*********靜態方法和實例方法無競爭**********/ //synLockTest.testClassAndObjClock(new SynchronizedObj()); /*****鎖住資源時沒有競爭,除非另一個方法也鎖定相同的資源*****/ //synLockTest.testLockSource(new SynchronizedObj()); /****加在this等於獲取對象所,和此對象全部實例方法競爭****/ synLockTest.testLockThis(new SynchronizedObj()); } /** * 對象鎖 * @param obj */ public void testObjClock(final SynchronizedObj obj){ Thread t1=new Thread(new Runnable() { @Override public void run() { obj.testWait(); } }); Thread t2=new Thread(new Runnable() { @Override public void run() { obj.testBlockWait(); } }); t1.start(); t2.start(); } /** * 類鎖 */ public void testClassClock(){ Thread t1=new Thread(new Runnable() { @Override public void run() { SynchronizedObj.testStaticWait(); } }); Thread t2=new Thread(new Runnable() { @Override public void run() { SynchronizedObj.testStaticBlockWait(); } }); t1.start(); t2.start(); } public void testClassAndObjClock(final SynchronizedObj obj){ Thread t1=new Thread(new Runnable() { @Override public void run() { obj.testWait(); } }); Thread t2=new Thread(new Runnable() { @Override public void run() { SynchronizedObj.testStaticWait(); } }); t1.start(); t2.start(); } public void testLockSource(final SynchronizedObj obj){ Thread t1=new Thread(new Runnable() { @Override public void run() { obj.testWait(); } }); Thread t2=new Thread(new Runnable() { @Override public void run() { SynchronizedObj.testStaticWait(); } }); Thread t3=new Thread(new Runnable() { @Override public void run() { obj.plus(); } }); t3.start(); t2.start(); t1.start(); } public void testLockThis(final SynchronizedObj obj){ Thread t1=new Thread(new Runnable() { @Override public void run() { obj.testWait(); } }); Thread t2=new Thread(new Runnable() { @Override public void run() { SynchronizedObj.testStaticWait(); } }); Thread t3=new Thread(new Runnable() { @Override public void run() { obj.plus_(); } }); t3.start(); t2.start(); t1.start(); } }