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

如下代码:
运行结果:

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;

    }

}

 

你可能感兴趣的:(基本数据类型)