java.util.Collections与线程安全

   在看Java Concurrency In Practice时看到Collections.unmodifiableMap的使用,于是去看了下Collections的源码,其中几个知识点做一个记录:

首先关于static修饰类:

摘自http://stackoverflow.com/questions/7486012/static-classes-in-java:

Can a class be static in Java ?The answer is YES, we can have static class in java. In java, we have static instance variables as well as static methods and also static block. Classes can also be made static in Java.

Java allows us to define a class within another class. Such a class is called a nested class. The class which enclosed nested class is known as Outer class. In java, we can’t make Top level class static. Only nested classes can be static.

What are the differences between static and non-static nested classes? Following are major differences between static nested class and non-static nested class. Non-static nested class is also called Inner Class.

1) Nested static class doesn’t need reference of Outer class, but Non-static nested class or Inner class requires Outer class reference.

2) Inner class(or non-static nested class) can access both static and non-static members of Outer class. A static class cannot access non-static members of the Outer class. It can access only static members of Outer class.

3) An instance of Inner class cannot be created without an instance of outer class and an Inner class can reference data and methods defined in Outer class in which it nests, so we don’t need to pass reference of an object to the constructor of the Inner class. For this reason Inner classes can make program simple and concise.


/* Java program to demonstrate how to implement static and non-static
   classes in a java program. */
class OuterClass{
   private static String msg = "GeeksForGeeks";

   // Static nested class
   public static class NestedStaticClass{

       // Only static members of Outer class is directly accessible in nested 
       // static class 
       public void printMessage() {

         // Try making 'message' a non-static variable, there will be 
         // compiler error  
         System.out.println("Message from nested static class: " + msg); 
       }
    }

    // non-static nested class - also called Inner class
    public class InnerClass{

       // Both static and non-static members of Outer class are accessible in 
       // this Inner class
       public void display(){
          System.out.println("Message from non-static nested class: "+ msg);
       }
    }
} 

class Main
{
    // How to create instance of static and non static nested class?
    public static void main(String args[]){

       // create instance of nested Static class
       OuterClass.NestedStaticClass printer = new OuterClass.NestedStaticClass();

       // call non static method of nested static class
       printer.printMessage();   

       // In order to create instance of Inner class we need an Outer class 
       // instance. Let us create Outer class instance for creating 
       // non-static nested class
       OuterClass outer = new OuterClass();        
       OuterClass.InnerClass inner  = outer.new InnerClass();

       // calling non-static method of Inner class
       inner.display();

       // we can also combine above steps in one step to create instance of 
       // Inner class
       OuterClass.InnerClass innerObject = new OuterClass().new InnerClass();

       // similarly we can now call Inner class method
       innerObject.display();
    }
}

有了上面的知识,看看Collections中的实现,Collections.unmodifiable*都是内部的static类,然后通过将普通的集合类型转化为unmodifiable的类型,将其中的修改操作屏蔽掉,而屏蔽的防止也很直接--抛出异常。

Java Concurrency In Practice提到过,保障线程安全的几种做法,其中最简单的方式就是实现状态的不可变性。但是不可变性是通过多方面综合作用的,特别是在涉及到集合时,往往涉及到安全发布的问题。只保证集合引用的不变性是不够的,还要保证集合内引用的状态安全不可变,这也就是为什么在处理状态时要做deepcopy的原因,通过返回给调用者一个新的对象从而保证共享状态的安全。

Collections.unmodifiable*的做法就是给你一个离线的只读不写的集合对象。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值