Summary of vector and stack source code analysis / list collection

Preface

This article is the last one of the collections under the list interface. The ArrayList and LinkedList have been explained before, leaving only vector and the subclass stack of vector. keep trying. work steadily,

                                                        --WZY

extend

Learning vector requires some multithreading knowledge. It is complicated here. I will mainly explain what will be used later

1. Lock mechanism: object lock, method lock and class lock

Object lock is method lock: it is to add the synchronized keyword to the method in a class, which is to lock the method.

Class lock: the lock is the whole class. When multiple threads declare the object of this class, it will be blocked until the object with this class lock is destroyed or the class lock is released actively. At this time, the blocked thread is selected to hold the lock of the class and declare the object of the class. Other threads continue to be blocked. For example, if there is a keyword synchronized on Class A, it is to add a class lock to class A. if thread 1 declares an instance of this class first, thread 1 gets the class lock, and thread 2 wants to declare an object of class A, it will be blocked.

2. In this paper, the method lock is used.

3. Each object has only one lock, including thread a, thread B, and a set C class. Thread a operates C to get the lock in the set (modified with the synchronized keyword in set C) and has not been executed yet, then thread a will not release the lock. When it is thread B's turn to operate the methods in set C, it is found that the lock has been taken away, Therefore, thread B can only wait for the thread that gets the lock to be used up, and then can get the lock for corresponding operations.

Here is just a brief introduction to a lock. This article is enough. If you don't think it's enough, you can go to Baidu first or check the problem of multi-threaded lock

1、 Initial vector (view API documentation)

A screenshot is for your reference. By reading the API, we can know the following points

1. Vector is an array with variable length

2. The length of vector is increased through the two variables of capacity and capacityincrement. At present, we don't know how to realize automatic amplification. We will analyze the source code later

3. Vector can also obtain iterators such as iterator and listiterator, and what happens to them is fail fast, not fail safe. Note that this vector is not mistaken because it is thread safe. The specific analysis will be described below

4. Vector is a thread safe class. If thread safety is required, use vector. If not, use ArrayList

5, as like as two peas, Vector and ArrayList are very different, from the inherited class and the interface of implementation, it is exactly the same as arrayList.

The current version is jdk1 7. There is a higher jdk1 8. In the development, it is recommended not to use vector. The reason will be explained at the end of the article. If thread safe collection classes are required, Java util. Class under concurrent package.

2、 Inheritance structure of vector

Two pictures are as like as two peas to suppress the shock. This is not the same as the ArrayList. If you don't understand, you can go there and see clearly.

         http://www.cnblogs.com/whgk/p/6079212.html

         http://www.cnblogs.com/whgk/p/6081526.html#3560719

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>