上代码,可直接跑,思路是用一个对象,写了同步对象方法,同步静态方法,以及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(); } }