java高并发(八)不可变对象

有一种对象一旦发布了,那么他就是安全对象,这就是不可变对象。

不可变对象需要满足的条件:

  • 对象创建以后其状态就不能修改
  • 对象所有的域都是final类型
  • 对象是正确创建的(在对象创建期间,this引用没有逸出)

final关键字:类、方法、变量

  • 修饰类:不能被继承。final类中的成员变量可以根据需要设置为final,要注意的是final类中的所有成员方法都会被隐式的指定为final方法。
  • 修饰方法:1. 锁定方法不被继承类修改;2. 效率
  • 修饰变量:基本数据类型变量在初始化之后就不能修改了,引用类型变量在初始化之后便不能指向另外一个对象

 下面举例说明final修饰变量:

@Slf4j
@NotThreadSafe
public class ImmutableExample1 {

    private final static Integer a = 1;
    private final static String b = "2";
    private final static Map<Integer, Integer> map = new HashMap<>();

    static {
        map.put(1, 2);
        map.put(2, 3);
    }

    public static void main(String[] args) {
//        a = 2;
//        b = "3";
//        map  = new HashMap<>();
        map.put(1, 3);
        log.info("{}", map.get(1));
    }

}

map引用变量不可以指定新的引用,但却可以修改里面的值。

这样就会引发线程安全方面的问题。

除了final定义不可变对象,是否还有其他手段定义不可变对象?当然可以

  • Collections.unmodifiableXX: Collection、List、Set、Map......
  • Guava:ImmutableXXX:Collection、List、Set、Map
@Slf4j
@ThreadSafe
public class ImmutableExample2 {

    private static Map<Integer, Integer> map = new HashMap<>();

    static {
        map.put(1, 2);
        map.put(2, 3);
        map = Collections.unmodifiableMap(map);
    }

    public static void main(String[] args) {
        map.put(1, 3);
        map.put(3,4);
        log.info("{}", map.get(3));
    }
}

 这样运行就会报错:

Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.Collections$UnmodifiableMap.put(Collections.java:1457)
	at com.vincent.example.immutable.ImmutableExample2.main(ImmutableExample2.java:24)

也就是说用Collections.unmodifiableMap声明一个变量,他的内容就不可以修改了。数据不会被污染。

@ThreadSafe
public class ImmutableExample3 {

    private final static ImmutableList<Integer> list = ImmutableList.of(1,2,3);
    private final static ImmutableSet<Integer> set = ImmutableSet.copyOf(list);
    private final static ImmutableMap<Integer, Integer> map = ImmutableMap.of(1,2,3,4);
    private final static ImmutableMap<Integer, Integer> map2 = ImmutableMap.<Integer, Integer>builder().put(1,2).put(3,4).build();

    public static void main(String[] args) {
        map2.put(4,5);
    }
}

根据变量实际情况变成最好变成不可变对象,如果可以尽量把对象变成不可变对象,这样在多线程情况下就不会出现线程安全问题了。

转载于:https://my.oschina.net/duanvincent/blog/3078977

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值