Several Java interview questions and short answers for record keeping

Talk about your understanding of how Spring works

   Quoting the explanation of a blog, https://www.cnblogs.com/xdp-gacl/p/4249939.html , it is very comprehensive and rich in content.

The difference between cookie and session

      1 The session is stored on the server, and the client does not know the information; the cookie is stored on the client, and the server can know the information

  2 The object is saved in the session, and the string is saved in the cookie

  3 Sessions cannot distinguish paths. When the same user visits a website, all sessions can be accessed anywhere; and if the path parameter is set in the cookie, the cookies under different paths in the same website are mutually inaccessible. Morality.

Three talk about the java memory model

      temporarily omitted

Several states of four Java threads

1. New (NEW): A new thread object is created.

2. RUNNABLE: After the thread object is created, other threads (such as the main thread) call the start() method of the object. Threads in this state are in the runnable thread pool, waiting to be selected by thread scheduling to obtain the right to use the CPU.

3. RUNNING: The thread in the runnable state obtains the cpu timeslice and executes the program code.

4. Blocked (BLOCKED): The blocked state means that the thread gives up the right to use the cpu for some reason, that is, gives up the cpu timeslice and temporarily stops running. Until the thread enters the runnable state, there is no chance to obtain the cpu timeslice again and go to the running state. There are three types of blocking: 

(1). Waiting for blocking: The running thread executes the o.wait() method, and the JVM puts the thread in the waiting queue.
(2) Synchronization blocking: When the running thread acquires the synchronization lock of the object, if the synchronization lock is occupied by another thread, the JVM will put the thread into the lock pool (lock pool).
(3). Other blocking: When the running thread executes the Thread.sleep(long ms) or t.join() method, or issues an I/O request, the JVM will place the thread in a blocking state. When the sleep() state times out, join() waits for the thread to terminate or times out, or when the I/O is processed, the thread re-enters the runnable state.

5. DEAD: When the execution of the run() and main() methods of the thread ends, or the run() method exits due to an exception, the thread ends its life cycle. Dead threads cannot be revived.

    Not to be confused with the thread state of the operating system.

5. Optimistic lock and pessimistic lock

   In relational database management systems, pessimistic concurrency control (also known as "pessimistic locking", Pessimistic Concurrency Control, abbreviated "PCC") is a method of concurrency control. It prevents a transaction from modifying data in a way that affects other users. If the operation performed by a transaction applies a lock to a row of data, only when the transaction releases the lock, other transactions can perform operations that conflict with the lock.
Pessimistic concurrency control is primarily used in environments where data contention is high, and where the cost of using locks to protect data in the event of a concurrency conflict is less than the cost of rolling back a transaction.

   [The characteristic of pessimistic locks is to acquire locks first, and then perform business operations. That is, "pessimistic" believes that acquiring locks is very likely to fail. Therefore, it is necessary to ensure that the acquisition of locks is successful before performing business operations.

  In relational database management systems, optimistic concurrency control (also known as "optimistic locking", Optimistic Concurrency Control, abbreviated "OCC") is a method of concurrency control. It assumes that multi-user concurrent transactions will not affect each other during processing, and each transaction can process the part of the data affected by each other without generating locks. Before committing a data update, each transaction checks whether other transactions have modified the data after the transaction has read the data. If other transactions have updates, the committing transaction will be rolled back. Optimistic transaction control was first proposed by Professor HTKung.

  [Characteristics of optimistic locks, do business operations first, and do not take locks unless absolutely necessary. That is to say, "optimistic" thinks that most of the locks will be successful, so it is good to take the locks in the last step when the business operation needs to actually update the data.

Six How to determine whether there is a cycle in a linked list, and where is the entry point?

    ①Using  the chasing method, set two pointers s l o w , f a s t slow and fast, all start from the head pointer, and advance 1 step and 2 steps respectively each time. If there is a ring, the two meet;

If there is no ring, f a s t fast encounters NULL and exits . If there is no ring, the fast pointer reaches the end of the linked list before the slow pointer, and the program exits.

    ②Let   the meeting point in problem 1 be m 1 , assign p = m 1 , q = h , where h is the head node of the linked list, and then p , q move forward one step at a time,

The position where p , q p, q meet again is the entry node of the ring (the connection point of the ring).

   During the interview, the reasoning process was not answered.

   Here is a diagram to explain. I haven't tried a mathematically rigorous proof yet, I'll add it later.

   If there is an error, the reader can help point it out.

    

    The problem of principle, if there is no review for a period of time, it is easy to forget, which may be related to the failure to apply it in practice.

So if you have time, you should read more about it.

 

    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324937606&siteId=291194637