加锁问题,必须加锁在对象上或方法上,加在基本数据类型上无效
2015-05-30 18:11
483 查看
如下代码: 运行结果:
Thread-0 holds the locktrue
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at blockthread.DeadLock$Thread1.run(DeadLock.java:24)
main holds the lockfalse
Thread-0:99
Thread-1 holds the lockfalse
Thread-1:100
失败原因是: synchronized 只能同步对象或者方法,如果在基本数据类型上给它加锁,synchronized 无效。
package blockthread; public class DeadLock { public static Integer i=100; public static void main(String[] args) { Thread1 t1=new Thread1(); Thread2 t2=new Thread2(); t1.start(); t2.start(); System.out.println(Thread.currentThread().getName()+" holds the lock"+Thread.holdsLock(i)); } static class Thread1 extends Thread{ public void run(){ while(true){ synchronized(i){ System.out.println(this.getName()+" holds the lock"+Thread.holdsLock(i)); if(i>0) i--; System.out.println(this.getName()+":"+i); i.notify(); try { i.wait(); System.out.println("线程1 等待"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //i.notify(); } } } static class Thread2 extends Thread{ public void run(){ while(true){ synchronized(i){ System.out.println(this.getName()+" holds the lock"+Thread.holdsLock(i)); if(i<100) i++; System.out.println(this.getName()+":"+i); i.notify(); try { i.wait(); System.out.println("线程2等待"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //i.notify(); } } } }
代码改为如下: 正常运行。
package blockthread; public class DeadLock { public static void main(String[] args) { Instance instance=new Instance(); Thread1 t1=new Thread1(instance); Thread2 t2=new Thread2(instance); t1.start(); t2.start(); System.out.println(Thread.currentThread().getName()+" holds the lock"+Thread.holdsLock(instance)); } static class Thread1 extends Thread{ private Instance instance; public Thread1(Instance instance){ this.instance=instance; } public void run(){ while(true){ synchronized(instance){ System.out.println(this.getName()+" holds the lock"+Thread.holdsLock(instance)); if(instance.getI()>0){ int i=instance.getI(); i--; } System.out.println(this.getName()+":"+instance.getI()); instance.notify(); try { instance.wait(); System.out.println("线程1 等待"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //i.notify(); } } } static class Thread2 extends Thread{ private Instance instance; public Thread2(Instance instance){ this.instance=instance; } public void run(){ while(true){ synchronized(instance){ System.out.println(this.getName()+" holds the lock"+Thread.holdsLock(instance)); if(instance.getI()<100){ int i=instance.getI(); i++; } System.out.println(this.getName()+":"+instance.getI()); instance.notify(); try { instance.wait(); System.out.println("线程2等待"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //i.notify(); } } } } class Instance{ private int i=100; public int getI() { return i; } public void setI(int i) { this.i = i; } }
相关文章推荐
- DJ's WebGL Tutorial 001--渲染准备
- android Spinner(下拉列表)
- 为家居、建材O2O业务,齐家网重金砸向家装O2O?
- lua路上踩到的坑
- javascript客户端检测技术
- Android自定义ScrollView
- iOS PerformSelector may cause a leak because its selector is unknown
- Redis相关指令文档
- mysql 事务隔离级别
- Android Studio 1.3下载地址
- 《金融史》阅读笔记
- ZigBee之基础概念2
- 字符串转化为Json格式的数据--(记录四)
- 4-1.最大子数组分治法实现
- XenServer 6.5实战系列之九:Creating a VM Template from a VM Snapshot
- lib_mysqludf_json 把mysql关系型数据转换成json格式的UDF工具
- 解决jqueryUI的拖拽,如果元素中含有iframe,拖动卡的问题
- Broadcast
- The JSP specification requires that an attribute name is preceded by whitespace
- 两种创建画布方法的差别