您的位置:首页 > 其它

加锁问题,必须加锁在对象上或方法上,加在基本数据类型上无效

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: