线程<一>不可变类

  • immutable
  • String??

immutable特点

1.类添加final修饰符,保证类不被继承。
2.保证所有成员变量必须私有,并且加上final修饰
3.不提供改变成员变量的方法,包括setter。
4.通过构造器初始化所有成员,进行深拷贝(deep copy)
5.在getter方法中,不要直接返回对象本身,而是克隆对象,并返回对象的拷贝(防止内存泄漏)

举例:
Collections工具类里的方法:

    public static  List unmodifiableList(List list) {
        return (list instanceof RandomAccess ?
                new UnmodifiableRandomAccessList<>(list) :
                new UnmodifiableList<>(list));

如:UnmodifiableList

static class UnmodifiableList extends UnmodifiableCollection
                                  implements List {
        private static final long serialVersionUID = -283967356065247728L;

        final List list;

        UnmodifiableList(List list) {
            super(list);
            this.list = list;
        }

        public boolean equals(Object o) {return o == this || list.equals(o);}
        public int hashCode()           {return list.hashCode();}

        public E get(int index) {return list.get(index);}
        public E set(int index, E element) {
            throw new UnsupportedOperationException();
        }
        public void add(int index, E element) {
            throw new UnsupportedOperationException();
        }
        public E remove(int index) {
            throw new UnsupportedOperationException();
        }
        public int indexOf(Object o)            {return list.indexOf(o);}
        public int lastIndexOf(Object o)        {return list.lastIndexOf(o);}
        public boolean addAll(int index, Collection c) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void replaceAll(UnaryOperator operator) {
            throw new UnsupportedOperationException();
        }
        @Override
        public void sort(Comparator c) {
            throw new UnsupportedOperationException();
        }

        public ListIterator listIterator()   {return listIterator(0);}

        public ListIterator listIterator(final int index) {
            return new ListIterator() {
                private final ListIterator i
                    = list.listIterator(index);

                public boolean hasNext()     {return i.hasNext();}
                public E next()              {return i.next();}
                public boolean hasPrevious() {return i.hasPrevious();}
                public E previous()          {return i.previous();}
                public int nextIndex()       {return i.nextIndex();}
                public int previousIndex()   {return i.previousIndex();}

                public void remove() {
                    throw new UnsupportedOperationException();
                }
                public void set(E e) {
                    throw new UnsupportedOperationException();
                }
                public void add(E e) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public void forEachRemaining(Consumer action) {
                    i.forEachRemaining(action);
                }
            };
        }

        public List subList(int fromIndex, int toIndex) {
            return new UnmodifiableList<>(list.subList(fromIndex, toIndex));
        }

        private Object readResolve() {
            return (list instanceof RandomAccess
                    ? new UnmodifiableRandomAccessList<>(list)
                    : this);
        }
    }

由代码可见,添加方法add等都抛异常。构造函数中的list也为final类型。
以此来保证不可变list。

你可能感兴趣的:(线程<一>不可变类)