5).脏读
public class PublicVar { public String username = "A"; public String password = "AA"; synchronized public void setValue(String username, String password){ try { this.username = username; Thread.sleep(5000); this.password = password; System.out.println("setvValue method thread name= " + Thread.currentThread().getName() + " username=" + username + " password="+ password); }catch (InterruptedException e){ e.printStackTrace(); } } public void getValue(){ System.out.println("getvValue method thread name= " + Thread.currentThread().getName() + " username=" + username + " password="+ password); } }
public class ThreadA extends Thread{ private PublicVar publicVar; public ThreadA(PublicVar publicVar) { this.publicVar = publicVar; } @Override public void run() { super.run(); publicVar.setValue("B","BB"); } }
public class Run { public static void main(String[] args) { try { PublicVar publicVar = new PublicVar(); ThreadA thread = new ThreadA(publicVar); thread.start(); Thread.sleep(200); //打印结果受此值大小影响 publicVar.getValue(); }catch (Exception e){ e.printStackTrace(); } } }
synchronized public void getValue(){ System.out.println("getvValue method thread name= " + Thread.currentThread().getName() + " username=" + username + " password="+ password); }
不在出现脏读了!!
6)synchronize锁重入
a.类内方法间锁重入
public class Service { synchronized public void service1() { System.out.println("service1"); service2(); } synchronized public void service2() { System.out.println("service2"); service3(); } synchronized public void service3() { System.out.println("service3"); } }
public class MyThread extends Thread{ @Override public void run() { Service service = new Service(); service.service1(); } }
public class Run { public static void main(String[] args) { MyThread t = new MyThread(); t.start(); } }
运行结果:
b.父子类继承中也有可重入锁
public class Main { public int i =10; synchronized public void operateMainMethod(){ try { i--; System.out.println("main print i=" + i); Thread.sleep(100); }catch (InterruptedException e){ e.printStackTrace(); } } }
public class Sub extends Main { synchronized public void operateSubMethod(){ try { while (i>0){ i--; System.out.println("sub print i="+i); Thread.sleep(100); this.operateMainMethod(); } }catch (InterruptedException e){ e.printStackTrace(); } } }
public class MyThread extends Thread { @Override public void run() { Sub sub = new Sub(); sub.operateSubMethod(); } }
public class Run { public static void main(String[] args) { MyThread thread = new MyThread(); thread.start(); } }
运行结果:
7)出现异常,锁自动释放
public class Service { synchronized public void testMetdod(){ if(Thread.currentThread().getName().equals("a")){ System.out.println("ThreadName=" + Thread.currentThread().getName() + "" + " run beginTime=" + System.currentTimeMillis()); int i =1; while(i == 1){ if(("" + Math.random()).substring(0,8).equals("0.123456")){ System.out.println("ThreadName=" + Thread.currentThread().getName() + " run exceptionTime=" + System.currentTimeMillis()); Integer.parseInt("a"); }else{ System.out.println("Thread B run Time=" + System.currentTimeMillis()); } } } } }
public class ThreadA extends Thread { private Service service; public ThreadA(Service service) { this.service = service; } @Override public void run() { service.testMetdod(); } }
public class ThreadB extends Thread { private Service service; public ThreadB(Service service) { this.service = service; } @Override public void run() { super.run(); } }
public class Run { public static void main(String[] args) { try { Service service = new Service(); ThreadA threadA = new ThreadA(service); threadA.setName("a"); threadA.start(); Thread.sleep(500); ThreadB b = new ThreadB(service); b.setName("b"); b.start(); }catch (InterruptedException e){ e.printStackTrace(); } } }
运行结果:
8)同步不具有继承性
public class Main { synchronized public void serviceMethod() { try { System.out.println("int main 下一步 sleep begin threadName=" + Thread.currentThread().getName() + " time==" + System.currentTimeMillis()); Thread.sleep(5000); System.out.println("int main 下一步 sleep end threadName=" + Thread.currentThread().getName() + " time==" + System.currentTimeMillis()); } catch (InterruptedException e) { e.printStackTrace(); } } }
public class Sub extends Main { @Override public void serviceMethod() { try { System.out.println("int sub 下一步 sleep begin threadName=" + Thread.currentThread().getName() + " time==" + System.currentTimeMillis()); Thread.sleep(5000); System.out.println("int sub 下一步 sleep end threadName=" + Thread.currentThread().getName() + " time==" + System.currentTimeMillis()); super.serviceMethod(); }catch (InterruptedException e){ e.printStackTrace(); } } }
public class MyThreadA extends Thread{ private Sub sub; public MyThreadA(Sub sub) { this.sub = sub; } @Override public void run() { sub.serviceMethod(); } }
public class MyThreadB extends Thread{ private Sub sub; public MyThreadB(Sub sub) { this.sub = sub; } @Override public void run() { sub.serviceMethod(); } }
public class Run { public static void main(String[] args) { try { Sub sub = new Sub(); MyThreadA myThreadA = new MyThreadA(sub); myThreadA.setName("a"); myThreadA.start(); Thread.sleep(1000); MyThreadB myThreadB = new MyThreadB(sub); myThreadB.setName("b"); myThreadB.start(); } catch (InterruptedException e) { e.printStackTrace(); } } }
运行结果:
说明:同步不能继承!!
修改子类为同步的方法。方法上加个同步关键字synchronize试试
public class Sub extends Main { @Override synchronized public void serviceMethod() { try { System.out.println("int sub 下一步 sleep begin threadName=" + Thread.currentThread().getName() + " time==" + System.currentTimeMillis()); Thread.sleep(5000); System.out.println("int sub 下一步 sleep end threadName=" + Thread.currentThread().getName() + " time==" + System.currentTimeMillis()); super.serviceMethod(); }catch (InterruptedException e){ e.printStackTrace(); } } }
运行结果:
同步了。完美!!