Synchronized(Integer对象){}(锁Integer类等不可变对象的坑) java.lang.IllegalMonitorStateException

下面写的代码是错误的,因为懒没有改,但是注视中写了解决方法

package com.Thread.ProducerConsumer;

/*
    利用生产者和消费者模式,完成以下输出:
    t1-->1
    t2-->2
    t1-->3
    t2-->4
    ......
    也就是满足,t1输出奇数,t2输出偶数。

    这样的写法是错误的,原因:
        Integer属于不可变类型,调用i++,就等于新建了一个Integer对象,所以是锁不住的。
    解决方法:可以自己写一个MyInteger类,来保存数字。
    class MyInteger{
        private int i;
        public MyInteger(int i){
            this.i = i;
        }
        public int getI(){
            return this.i;
        }
        public void setI(int i){
            this.i = i;
        }
    }
 */
public class Test2 {
    public static void main(String[] args) {
        int i = 1;
        //奇数线程
        Thread t1 = new Thread(new Odd(i));
        //偶数线程
        Thread t2 = new Thread(new Even(i));

        t1.setName("t1");
        t2.setName("t2");

        t1.start();
        t2.start();
    }
}

class Even implements Runnable {
    private Integer i;

    public Even(Integer i) {
        this.i = i;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (i) {
                if (i % 2 == 1) {
                    try {
                        i.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName() + "--->" + i);
                i = i + 1;
                i.notify();
            }
        }
    }
}

class Odd implements Runnable {
    private Integer i;

    public Odd(Integer i) {
        this.i = i;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (i) {
                if (i % 2 == 0) {
                    try {
                        i.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName() + "--->" + i);
                i = i + 1;
                i.notify();
            }
        }
    }
}

一定要记住   不可变类创建的对象是锁不住的,如果只有一个这样的对象,建议试用类锁

否则也会跳出 java.lang.IllegalMonitorStateException这样的错误。当然出现这个错误的原因有很多,这只是我目前遇到的解决方法。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值