在JVM中具有String常量池缓存的功能
package com.cky.test; /** * Created by edison on 2017/12/8. */ public class Test { public static void main(String[] args) { String a= "a"; String b= "a"; System.out.println(a==b); } }
true
将synchronized(string)同步代码块和String联合使用,结果出现意外
测试
package com.cky.bean; /** * Created by edison on 2017/12/8. */ public class Service { public static void print(String str) { try { synchronized (str) { while(true) { System.out.println(Thread.currentThread().getName()); Thread.sleep(1000); } } } catch (InterruptedException e) { e.printStackTrace(); } } }
package com.cky.thread; import com.cky.bean.Service; /** * Created by edison on 2017/12/8. */ public class ThreadA extends Thread{ private Service service; public ThreadA(Service service) { this.service = service; } @Override public void run() { super.run(); service.print("AA"); } }
package com.cky.thread; import com.cky.bean.Service; /** * Created by edison on 2017/12/8. */ public class ThreadB extends Thread{ private Service service; public ThreadB(Service service) { this.service = service; } @Override public void run() { super.run(); service.print("AA"); } }
package com.cky.test; import com.cky.bean.Service; import com.cky.thread.ThreadA; import com.cky.thread.ThreadB; /** * Created by edison on 2017/12/8. */ public class Test { public static void main(String[] args) { Service service = new Service(); ThreadA threadA = new ThreadA(service); threadA.setName("a"); threadA.start(); ThreadB threadB = new ThreadB(service); threadB.setName("b"); threadB.start(); } }
a
a
a
a
a
a
a
a
结果是无限循环的打印a
出现这种情况的原因是因为String的两个值都是AA,两个线程持有相同的锁。所以造成线程B不能执行,这就是String常量2池带来的问题,所以大多数情况下都不使用String做所对象,比如new Object实例化一个Object对象,但他不放入缓存中
测试
package com.cky.bean; /** * Created by edison on 2017/12/8. */ public class Service { public static void print(Object object) { try { synchronized (object) { while(true) { System.out.println(Thread.currentThread().getName()); Thread.sleep(1000); } } } catch (InterruptedException e) { e.printStackTrace(); } } }
package com.cky.thread; import com.cky.bean.Service; /** * Created by edison on 2017/12/8. */ public class ThreadA extends Thread{ private Service service; public ThreadA(Service service) { this.service = service; } @Override public void run() { super.run(); service.print(new Object()); } }
package com.cky.thread; import com.cky.bean.Service; /** * Created by edison on 2017/12/8. */ public class ThreadB extends Thread{ private Service service; public ThreadB(Service service) { this.service = service; } @Override public void run() { super.run(); service.print(new Object()); } }
package com.cky.test; import com.cky.bean.Service; import com.cky.thread.ThreadA; import com.cky.thread.ThreadB; /** * Created by edison on 2017/12/8. */ public class Test { public static void main(String[] args) { Service service = new Service(); ThreadA threadA = new ThreadA(service); threadA.setName("a"); threadA.start(); ThreadB threadB = new ThreadB(service); threadB.setName("b"); threadB.start(); } }
a
b
b
a
a
b
a
b
b
a
交替打印说明一个一个对象锁