java knowledge point one

Posted May 25, 20208 min read

  • hashMap

    The bottom layer of hashMap is an array + linked list structure, which uses key-value pairs to store data, and the initial capacity is
    16, when the used capacity of the array exceeds the actual capacity by more than 3/4, the capacity will be expanded.
    Is a multiple of 2, when the depth of the linked list on the array is greater than 8, the linked list will be converted into a red and black tree(improve the query
    effectiveness);

    Implementation:
    Hash algorithm and addressing algorithm:

      When put(key, value), hash the key((h =
      key.hashCode()) ^(h >>> 16)), the key takes the hash value and shifts it to the right by 16 bits, and the original hash value
      XOR(1 if not the same, 0 if the same), the formula of addressing(n -1) & hash(here refers to recalculation
      Hash), the reason for using & operation(the same is 1 when it is 1, otherwise 0), instead of taking the modulus, because &
      Higher efficiency, the reason why the above uses high 16-bit and low 16-bit XOR can effectively reduce hash collision
     (Some hash values   may be different in the upper 16 bits and the same in the lower 16 bits, so if you perform an & operation with n-1, the relative
     (When the upper 16 bits are not involved in the calculation);

    Hash collision resolution:

      Mainly through the mutual conversion of linked lists and red and black trees;

    Capacity expansion mechanism:

      Capacity expansion involves rehash, which consumes performance when the amount of data is large
  • concurrentHashMap source code analysis

    Hashmap has thread safety problems, so there will be concurrentHashMap to solve it;
    Two cases,

      1. Two threads put elements into the map at the same time, due to the conversion of the linked list, resulting in data loss;
      2. A thread put operation, a thread get operation, may get to the null value;

    The hashtable solution is too inefficient:use sychronized to lock the entire array;

    The principle of concurrentHashMap solution:use CAS, compare and swap;

  1. Array initialization guarantees thread safety:

     private final Node <K, V> []initTable() {
         Node <K, V> []tab; int sc;
         while((tab = table) == null || tab.length == 0) {
             if((sc = sizeCtl) <0)
                 Thread.yield(); //lost initialization race; just spin
             //use cas here to ensure thread safety during initial recognition
             else if(U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
                 try {
                     if((tab = table) == null || tab.length == 0) {
                         int n =(sc> 0)? sc:DEFAULT_CAPACITY;
                         @SuppressWarnings("unchecked")
                         Node <K, V> []nt =(Node <K, V> []) new Node <?,?> [N];
                         table = tab = nt;
                         sc = n-(n >>> 2);
                     }
                 } finally {
                     sizeCtl = sc;
                 }
                 break;
             }
         }
         return tab;
     }

Use of put method

Initialize the array is complete, but there is no value
    final V putVal(K key, V value, boolean onlyIfAbsent) {
        if(key == null || value == null) throw new NullPointerException();
        int hash = spread(key.hashCode());
        int binCount = 0;
        for(Node <K, V> []tab = table ;;) {
            Node <K, V> f; int n, i, fh;
            if(tab == null ||(n = tab.length) == 0)
                tab = initTable();
            else if((f = tabAt(tab, i =(n-1) & hash)) == null) {
                //use cas here
                if(casTabAt(tab, i, null,
                             new Node <K, V>(hash, key, value, null)))
                    break; //no lock when adding to empty bin
            }
            else if((fh = f.hash) == MOVED)
                tab = helpTransfer(tab, f);
            else {
               ... omit code
        }
        addCount(1L, binCount);
        return null;
    }

When the initialization array is completed, and there is a value, use a lock, this lock is actually only added to a link;
synchronized(f) {
               Code omitted ...
}

When expanding

When the first thread comes to expand capacity, it will modify a head node to -1, indicating that capacity is being expanded, and each time it is assigned to a thread with 16 array nodes from the tail. The previous thread performs rehash, he will allocate 16 nodes in front of the previous thread to operate;
Thread Pool

How the thread pool works

  • When the thread comes, it will first determine whether the core thread is full, if not, it will create a core thread to execute the task;
  • If the core thread is full, then he will put the task into the blocking queue;
  • When the blocking queue is also full, he will judge whether the thread pool also defines the number of non-core threads, if there are non-core threads, then create threads and execute tasks;
  • When the number of non-core threads is also full, the task rejection strategy we defined will be executed;

Thread pool rejection strategy

ThreadPoolExecutor.AbortPolicy:Discard the task and throw RejectedExecutionException.
ThreadPoolExecutor.DiscardPolicy:Discard the task, but do not throw an exception.
ThreadPoolExecutor.DiscardOldestPolicy:Discard the first task in the queue, and then resubmit the rejected task
ThreadPoolExecutor.CallerRunsPolicy:the calling thread(the thread that submitted the task) handles the task

What happens when the thread pool uses an unbounded blocking queue?

A scenario where when a remote call is blocked, the task in the queue is blocked
The more accumulated the more, the OOM problem finally appears;

Using a bounded queue, how to perform subsequent tasks when the queue and threads are full?

You can customize the rejection strategy to persist the tasks to disk, when the tasks in the queue are executed, they are obtained from the disk and executed;

What happens to the tasks in the queue after the machine goes down?

At this time, you can save the task to the database before submitting the task, and define the status(not submitted),
When the submission is complete and the operation is complete, modify the status(submitted);

When the program starts, define a background process, scan the database, remove any uncommitted tasks
Business to submit;
java memory model

Order

The operation of the program is carried out in sequence, here mainly involves the instruction rearrangement of jvm and cpu

Atomicity

When a thread updates an operation to share data, other threads are not allowed to perform update operations;

Visibility

The operation of a thread on a shared data can be seen by other threads. The memory level is that after a thread updates a data, it will refresh the data into the main memory. Read;

Principle of volatile keyword

The main principle is the above visibility problem. Volatile is mainly to update the data update of the thread to the main memory, and invalidate the memory of the thread that uses this data in other threads. Read from main memory;
TCP/IP network model

Physical layer

Simply put, it is wireless signals, network cables and submarine optical cables, etc., so that computers can be physically connected;

Data link layer

This layer is based on the physical layer, which is the data sent by one computer to another computer.
But there is a problem, the data sent by one computer is a series of signals, and the other computer does not
I do n t know what was sent to myself, and how to parse the data sent to myself, because
Some protocols were born, such as Ethernet and Internet:
1. Ethernet:a group of electrical signals is a data packet, which is a frame, each frame is divided into two
Part, head and data, some descriptive information in the head, where does the data come from,
And data information, data is real data, these data packets need to be from a computer
The network card is sent out, received by the network card of another computer, Ethernet regulations, each network card
There must be a unique identifier, which is the mac address, and the data packet of the network card is sent by broadcast
Is sent to all computers in the local area network, the receiver is based on the
Judge whether the message was sent to yourself;

Network layer

The data layer solves the problem of packet sending and parsing, but it cannot distinguish the partition of the network,
That is, those computers can communicate with each other to communicate, and there is an IP protocol in the network layer, which defines
The computer's IP address, the unique identifier of each computer, and to determine whether the computer is a subnet,
You also need a subnet mask, perform a binary operation on the ip address and subnet mask, and then pass
Use this binary number to determine which computers belong to a subnet;

When two computers are not in a local area network, how to communicate and connect, this time will use routing
The IP address and network card address of the computer can be registered on the router;

Transport layer

The above solves the connection between the computer and the computer, data transmission and identification problems, but each computer has different applications, and a computer has only one network card, so the concept of port number is introduced, each application Listen to different port numbers and receive messages. This layer establishes the TCP protocol(how to establish a connection, how to send and read messages, which is specified by the tcp protocol);

Application layer

What should I do with the data and how to parse it? This is what this layer does, specifically including the http protocol and ftp protocol ...
Approximate process when the browser requests a network address
1. First go to the DNS resolver to resolve the address to an IP address;
2. Take the HTTP protocol of the application layer, at this time, the data will be packaged according to the HTTP protocol;
3. Packaging transport layer, according to the tcp protocol, package tcp protocol data packets, including that machine
Port information;
4. Packaging network layer:packaging data packets according to the ip protocol for network transmission;
5. Pack the data link layer, encapsulate the data packet according to a network protocol, and define the data packet recipient
ip and sender ip etc .;
6. Send data through routing, switches, etc .;
7. The receiver unpacks the data layer by layer according to the protocol of each layer;
TCP three-way handshake/four-way wave

Three handshake

The first time:the client sends a request to the server, SYN = 1, ACK = 0, seq = x;
Second time:The server responds to the client, ack = x + 1, SYN = 1, ACK = 1, seq = y;
Third time:The client responds to the server, ack = y + 1, ACK = 1, seq = x + 1;

Why is it three times:mainly to confirm each other, the first time the client requests the service
The server establishes a connection, the second request is that the server responds to the client, and the client confirms
I can receive the server response, but at this time the server cannot determine whether the client
To continue the connection or whether the client's response can be received, it is necessary to confirm the third time;

Wave four times

The first wave of hands:the client sends a FIN segment to the server and sets seq to 100
And ack is 120; at this time, the client enters FIN \ _WAIT \ _1 state, which means that the client
There is no data to send to the server, request to close the connection;

The second wave:the server received the FIN segment sent by the client, and returned a
ACK segment, ack is set to 101, seq is set to 120; the server has entered
CLOSE \ _WAIT state, the client enters after receiving the ACK message returned by the server
FIN \ _WAIT \ _2 status;

The third wave:the server will observe whether there is still data not sent to the client, such as
If yes, send the data to the client first, and then send the FIN message; if not, then the service
The device sends the FIN message directly to the client. Request to close the connection while the server enters
LAST \ _ACK status;

The fourth wave:the client receives the FIN segment sent by the server and sends an ACK to the server
In the message segment, set seq to 101 and ack to 121, and then the client enters
TIME \ _WAIT state; after the server receives the client's ACK segment, it closes the connection;
At this time, the client has not received a response after waiting for 2MSL, which proves that the server has been normal
Closed, the client can also close the connection;
http works
HTTP requests are mainly divided into request headers and request bodies. There are status codes, request methods, request addresses and other information in the request headers. The request weight is mainly data, and the main process is the above browser request process;
How does Mybatis prevent SQL injection
Mainly through the difference between # {} and ${}, # {} will add double quotes to the SQL statement after the brackets, and ${} will directly add the content to the SQL statement In the former, the former will pre-compile the sql statement, the latter will not, so using # {} will not have the problem of sql injection, ${} will have the problem of sql injection. ;
CSRF attack and XSS attack
CSRF attack
Cross-site request forgery attacks, mainly to obtain the jsessionid of the client to simulate the client request server;
Solution:When returning the cookie, set the property to HttpOnly;

XSS attack
Similar to the above, except that the xss attack uses html + javascript scripts to load on the client computer for execution, usually using malicious links or writing scripts in the comments of the website
Solution:Cookie returned, set HttpOnly attribute, the content of the website determined by the customer and saved to the database for special character processing;
Distributed traffic attack
That is, the amount of concurrency that the server can bear is not high, hackers use multiple or high-performance servers, send a large number of requests to the server, and directly paralyze the service;

When the server endures a high amount of concurrency, there will also be a situation where hackers will air others' computers and launch a large number of attacks on the server end;