In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "the difference between SynchronizedList and Vector in java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the difference between SynchronizedList and Vector in java.
Preface
Vector is a class in the java.util package. SynchronizedList is a static inner class in java.util.Collections.
You can use the Vector class directly in a multithreaded scenario, or you can use the Collections.synchronizedList (List list) method to return a thread-safe List.
So, is there any difference between SynchronizedList and Vector, and why does java api provide these two thread-safe List implementations?
First of all, we know that Vector and Arraylist are subclasses of List, and their underlying implementations are the same. So here's the difference between the two list1 and list2:
List list = new ArrayList (); List list2 = Collections.synchronizedList (list); Vector list1 = new Vector ()
First, compare several important methods
1.1 add method
Implementation of Vector:
Public void add (int index, E element) {insertElementAt (element, index);} public synchronized void insertElementAt (E obj, int index) {modCount++;if (index > elementCount) {throw new ArrayIndexOutOfBoundsException (index+ ">" + elementCount);} ensureCapacityHelper (elementCount+ 1); System.arraycopy (elementData, index, elementData, index+ 1, elementCount-index); elementData [index] = obj;elementCount++;} private void ensureCapacityHelper (int minCapacity) {/ / overflow-conscious codeif (minCapacity-elementData.length > 0) grow (minCapacity);}
Implementation of synchronizedList:
Public void add (int index, E element) {synchronized (mutex) {list.add (index, element);}}
Here, the add () method of ArrayList is called using synchronous code blocks. The add method of ArrayList is as follows:
Public void add (int index, E element) {rangeCheckForAdd (index); ensureCapacityInternal (size+ 1); / / Increments mod CountSystem.arraycopy (elementData, index, elementData, index + 1 department size-index); elementData [index] = element;size++;} private void rangeCheckForAdd (int index) {if (index > size | | index
< 0)throw new IndexOutOfBoundsException(outOfBoundsMsg(index));}private void ensureCapacityInternal(int minCapacity) {if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);}ensureExplicitCapacity(minCapacity);} 从上面两段代码中发现有两处不同: Vector使用同步方法实现,synchronizedList使用同步代码块实现。 两者的扩充数组容量方式不一样(两者的add方法在扩容方面的差别也就是ArrayList和Vector的差别。) 1.2 remove方法 synchronizedList的实现: public E remove(int index) {synchronized (mutex) {return list.remove(index);}} ArrayList类的remove方法内容如下: public E remove(int index) {rangeCheck(index);modCount++;E oldValue = elementData(index);int numMoved = size - index - 1;if (numMoved >0) System.arraycopy (elementData, index+1, elementData, index,numMoved); elementData [--size] = null; / / clear to let GC do its workreturn oldValue;}
Implementation of Vector:
Public synchronized E remove (int index) {modCount++;if (index > = elementCount) throw new ArrayIndexOutOfBoundsException (index); E oldValue = elementData (index); int numMoved = elementCount-index-1 political if (numMoved > 0) System.arraycopy (elementData, index+1, elementData, index,numMoved); elementData [--elementCount] = null; / Let gc do its workreturn oldValue;}
From the remove method, we find that there is almost no difference except that one uses the synchronization method and the other uses the synchronization code block.
By comparing other methods, we find that almost all the methods implemented in SynchronizedList use the method of synchronizing the List on the code block package. If the List is ArrayList, then one of the obvious differences between SynchronizedList and Vector is that one uses synchronization blocks and the other uses synchronization methods.
Second, differential analysis
Data growth difference
From the internal implementation mechanism, both ArrayList and Vector use Array to control the objects in the collection. When you add elements to these two types, if the number of elements exceeds the current length of the internal array, they both need to expand the length of the internal array. Vector automatically doubles the length of the internal array by default, and the ArrayList is 50% of the original, so in the end, the set you get always takes up more space than you actually need. So if you want to save a large amount of data in a collection, then using Vector has some advantages, because you can avoid unnecessary resource overhead by setting the initialization size of the collection.
The difference between synchronization code blocks and synchronization methods
The scope of the synchronization code block may be smaller than that of the synchronization method, which is generally inversely proportional to the performance of the lock. The synchronization block can more precisely control the scope of the lock (the scope of the lock is from the time the lock is acquired to the time it is released), and the scope of the lock of the synchronization method is the whole method. Static code blocks can choose which object to lock, but static methods can only lock this objects.
Because SynchronizedList simply wraps the method of ArrayList with synchronous code blocks, and the method body content of the method of the same name in ArrayList and Vector is not much different, there is no difference in the scope of the lock and the scope of the lock. In terms of the difference between locked objects, SynchronizedList's synchronization code block locks mutex objects and Vector locks this objects. So what is the mutex object? In fact, SynchronizedList has a constructor that can pass in an Object. If an object is passed in when it is called, then the object passed in by the user is locked. If not specified, the this object is also locked.
So, there are two differences between SynchronizedList and Vector so far:
If you use the add method, then their expansion mechanism is different. SynchronizedList can specify locked objects.
But there are buts in everything. Not all classes implemented in SynchronizedList use synchronized to synchronize code blocks. Among them, listIterator and listIterator (int index) are not synchronized. However, Vector adds a method lock to the method. So lock it manually when traversing with SynchronizedList.
But, but then there are buts.
Previous comparisons are based on our conversion of ArrayList to SynchronizedList. So if we want to make LinkedList thread-safe, or I want synchronous linked lists that make it easy to insert and delete in the middle, then I can convert the existing LinkedList directly to SynchronizedList without changing its underlying data structure. Vector cannot do this because his underlying structure is implemented using arrays, which cannot be changed.
So, finally, the main difference between SynchronizedList and Vector:
SynchronizedList has good extensibility and compatibility. He can convert all subclasses of List to thread-safe classes. When using SynchronizedList, the traversal should be synchronized manually. SynchronizedList can specify locked objects.
At this point, I believe you have a deeper understanding of "the difference between SynchronizedList and Vector in java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.