标签:
1 package concurrent; 2 /** 3 * 测试不同线程操作同一个实例变量线程安全问题 4 * @author foolishbird_lmy 5 * 6 */ 7 class ThePrivateNumber{ 8 private int num = 0; 9 10 public synchronized void addI(String username){ 11 try { 12 if (username.equals("a" )){ 13 num = 100; 14 System. out .println("a set over!"); 15 Thread. sleep(2000); 16 } else { 17 num = 200; 18 System. out .println("b set over!"); 19 } 20 System. out .println(Thread.currentThread().getName()+ " num is " +num ); 21 } catch (InterruptedException e){ 22 e.printStackTrace(); 23 } 24 } 25 } 26 27 class ThePrivateNumA extends Thread{ 28 private ThePrivateNumber tNumber; 29 public ThePrivateNumA(ThePrivateNumber tNumber){ 30 this .tNumber = tNumber; 31 } 32 public void run(){ 33 tNumber.addI( "a" ); 34 } 35 } 36 class ThePrivateNumB extends Thread{ 37 private ThePrivateNumber tNumber; 38 public ThePrivateNumB(ThePrivateNumber tNumber){ 39 this .tNumber = tNumber; 40 } 41 public void run(){ 42 tNumber.addI( "b" ); 43 } 44 } 45 public class ThePrivateNum { 46 public static void main(String[] args) { 47 ThePrivateNumber tNumber = new ThePrivateNumber(); 48 ThePrivateNumA tNumA = new ThePrivateNumA(tNumber); 49 tNumA.setName( "A" ); 50 tNumA.start(); 51 ThePrivateNumB tNumB = new ThePrivateNumB(tNumber); 52 tNumB.setName( "B" ); 53 tNumB.start(); 54 } 55 }
1 package concurrent; 2 3 class MySynchroized{ 4 public synchronized void methodA(){ 5 try { 6 System. out .println("the methodA is start:"+Thread. currentThread().getName()); 7 Thread. sleep(5000); 8 System. out .println("A end time "+System.currentTimeMillis ()); 9 } catch (InterruptedException e){ 10 e.printStackTrace(); 11 } 12 } 13 //synchronized,分别测试加同步锁与不加执行顺序 14 public void methodB(){ 15 try { 16 System. out .println("the methodB is start:"+Thread. currentThread().getName()); 17 System. out .println("B begin time "+System.currentTimeMillis ()); 18 Thread. sleep(5000); 19 } catch (InterruptedException e){ 20 e.printStackTrace(); 21 } 22 } 23 } 24 class TestA extends Thread{ 25 private MySynchroized ms; 26 public TestA(MySynchroized ms){ 27 this .ms = ms; 28 } 29 public void run(){ 30 ms.methodA(); //调用同步方法 31 } 32 } 33 class TestB extends Thread{ 34 private MySynchroized ms; 35 public TestB(MySynchroized ms){ 36 this .ms = ms; 37 } 38 public void run(){ 39 ms.methodB(); //调用非同步方法 40 } 41 } 42 public class TestSynchroized { 43 public static void main(String[] args) { 44 MySynchroized ms = new MySynchroized(); 45 TestA a = new TestA(ms); 46 a.setName( "A" ); 47 TestB b = new TestB(ms); 48 b.setName( "B" ); 49 a.start(); 50 b.start(); 51 } 52 }
1 package concurrent; 2 class DirtyRead { 3 private String name = "a" ; 4 private String id = "aa" ; 5 public synchronized void set(String name, String id) { 6 try { 7 this .name = name; 8 Thread. sleep(2000); 9 this .id = id; 10 System. out .println("set method " + Thread.currentThread().getName() 11 + " name:" + name + " id:" + id); 12 } catch (InterruptedException e) { 13 e.printStackTrace(); 14 } 15 } 16 public void get() { 17 System. out .println("get method " + Thread.currentThread().getName() 18 + " name:" + name + " id:" + id ); 19 } 20 } 21 class DirtyReadRun extends Thread { 22 private DirtyRead dr; 23 public DirtyReadRun(DirtyRead dr) { 24 this .dr = dr; 25 } 26 public void run() { 27 dr.set( "b" , "bb" ); 28 } 29 } 30 public class TestDirtyRead { 31 public static void main(String[] args) { 32 try { 33 DirtyRead dr = new DirtyRead(); 34 DirtyReadRun drr = new DirtyReadRun(dr); 35 drr.start(); 36 // Thread.sleep(1000); 37 Thread. sleep(3000); 38 dr.get(); 39 } catch (InterruptedException e) { 40 e.printStackTrace(); 41 } 42 } 43 }
1 package concurrent; 2 /** 3 * 可重入锁测试 4 * @author foolishbird_lmy 5 * 6 */ 7 class Synch{ 8 public synchronized void sA(){ 9 System. out .println("sA()" ); 10 sB(); 11 } 12 public synchronized void sB(){ 13 System. out .println("sB()" ); 14 sC(); 15 } 16 public synchronized void sC(){ 17 System. out .println("sC()" ); 18 } 19 } 20 class SynchARun extends Thread{ 21 private Synch sa; 22 public SynchARun(Synch sa){ 23 this .sa = sa; 24 } 25 public void run(){ 26 sa.sA(); 27 } 28 } 29 public class ReSynchronized { 30 public static void main(String[] args) { 31 SynchARun sa = new SynchARun( new Synch()); 32 sa.start(); 33 } 34 }
从输出结果可以看出,线程只要获取到了该对象锁,其他的同步锁也一样能获取。
五、静态同步synchronized方法与synchronized(class)代码块
用static修饰的同步方法中的同步锁是给Class类上锁,而非static方法是给对象上锁;
1 package concurrent; 2 3 class StaticSyn { 4 public static synchronized void printA() { 5 try { 6 System. out .println(Thread.currentThread().getName() + " : " 7 + System.currentTimeMillis() + "进入printA()"); 8 Thread. sleep(3000); 9 System. out .println(Thread.currentThread().getName() + " : " 10 + System.currentTimeMillis() + "退出printA()"); 11 } catch (InterruptedException e) { 12 e.printStackTrace(); 13 } 14 } 15 public static synchronized void printB() { 16 try { 17 System. out .println(Thread.currentThread().getName() + " : " 18 + System.currentTimeMillis() + "进入printB()"); 19 Thread. sleep(3000); 20 System. out .println(Thread.currentThread().getName() + " : " 21 + System.currentTimeMillis() + "退出printB()"); 22 } catch (InterruptedException e) { 23 e.printStackTrace(); 24 } 25 } 26 public synchronized void printC() { 27 try { 28 System. out .println(Thread.currentThread().getName() + " : " 29 + System.currentTimeMillis() + "进入printC()"); 30 Thread. sleep(3000); 31 System. out .println(Thread.currentThread().getName() + " : " 32 + System.currentTimeMillis() + "退出printC()"); 33 } catch (InterruptedException e) { 34 e.printStackTrace(); 35 } 36 } 37 } 38 class StaticSynRunA extends Thread{ 39 private StaticSyn ss; 40 public StaticSynRunA(StaticSyn ss){ 41 this .ss = ss; 42 } 43 @SuppressWarnings( "static-access" ) 44 public void run(){ 45 ss. printA(); 46 } 47 } 48 class StaticSynRunB extends Thread{ 49 private StaticSyn ss; 50 public StaticSynRunB(StaticSyn ss){ 51 this .ss = ss; 52 } 53 public void run(){ 54 ss .printB (); 55 } 56 } 57 class StaticSynRunC extends Thread{ 58 private StaticSyn ss; 59 public StaticSynRunC(StaticSyn ss){ 60 this .ss = ss; 61 } 62 public void run(){ 63 ss.printC(); 64 } 65 } 66 public class TestStaticSyn { 67 public static void main(String[] args) { 68 StaticSyn ss = new StaticSyn(); 69 StaticSynRunA ssa = new StaticSynRunA(ss); 70 ssa.setName( "A" ); 71 ssa.start(); 72 StaticSynRunB ssb = new StaticSynRunB(ss); 73 ssb.setName( "B" ); 74 ssb.start(); 75 StaticSynRunC ssc = new StaticSynRunC(ss); 76 ssc.setName( "C" ); 77 // ssc.start(); 78 } 79 }
标签:
原文地址:http://www.cnblogs.com/lmy-foolishbird/p/5471853.html