This issue is the 4 issue of the [ interview with a large factory ] series. The title comes from the second side of Meituan to the store.

Interview scene

Interviewer : First of all, what is the difference between a process and a thread?

Monologue: Old Baguwen Haha

: A process is an independent unit for the system to allocate and schedule resources, and each process has its own memory space and system resources

: A thread is an entity of a process and the basic unit of CPU scheduling and dispatch. It is a basic unit smaller than a process that can run independently

: Multithreading is an effective means to realize concurrency mechanism. Processes, like threads, are the basic unit of concurrency

Interviewer : Then why use multithreading?

Monologue: Hey, this is easy

: The main reason for using multithreading is to improve system resource utilization.

: Multiple threads run at the same time, which can reduce the overhead of thread context switching, improve the concurrency capability and CPU utilization efficiency.

: Multithreading is also common in normal work. For example, every time Tomcat processes a request, it will take a thread from the thread connection pool to process it.

Interviewer : Well, when you use multithreading, you may encounter thread safety issues. Talk about what is thread safety?

: This is how I understand it. When multiple threads access an object, if you don't need to consider the scheduling and alternate execution of these threads in the runtime environment, you don't need to perform additional synchronization. The behavior of calling this object is all The correct result can be obtained, then the object is thread-safe.

Interviewer : How do you usually deal with thread safety issues?

: This has to be analyzed in detail. First, determine whether there is a thread safety problem, and if so, deal with the thread safety problem according to the specific situation.

: For example, when it comes to the atomicity of operations, consider using the atomic class under the atomic package.

: If it involves control of threads, you can consider thread tool classes CountDownLatch / Semaphore and so on.

: For the collection class, consider the collection class under the java.util.concurrent package.

: There are also classes under the synchronized and lock packages, redis distributed locks, etc.

Interviewer : , I just mentioned Redis distributed locks, what scenarios do you think need to use distributed locks?

: In a stand-alone environment, thread safety problems can be avoided by ReentrantLock , synchronized , and concurrent with some thread-safe classes.

: In a multi-machine deployment environment, thread security needs to be ensured in multiple processes. These APIs provided by Java can only ensure thread security for multi-threaded access to shared resources within a single JVM process, which is no longer sufficient. . At this time, you need to use distributed locks to ensure thread safety.

: In versions before Redis 2.6.12, setnx + expire was used to implement distributed locks. After Redis 2.6.12 version, setnx added the expiration time parameter, and only need to use setnx to realize distributed lock.

Interviewer : Then talk about the principle of Redis distributed lock?

Monologue: Interview to build a rocket, start a job and screw a screw?

: First, let's introduce the locking logic of Redis.

: setnx for the lock of the key. If the key already exists, no operation will be performed, and retry will continue after a period of time to ensure that only one client can hold the lock.

: The value is set to requestId (you can use the machine ip to splicing the current thread name), indicating which request the lock is added to. When unlocking, you need to determine whether the current request holds the lock to prevent accidental unlocking. For example, when client A is locked, the lock expires before the unlock is executed. At this time, client B tries to lock successfully, and then client A executes the del() method to release the lock of client B.

: Use expire to add an expiration time to the lock to prevent the lock from not being released due to exceptions.

: Then there is the unlocking logic.

: First obtain the value corresponding to the lock, check whether it is equal to requestId , and delete the lock if it is equal. Use lua scripts to implement atomic operations to ensure thread safety.

Interviewer : Yes, I see that you are familiar with TCP on your resume, so let's introduce the four waves of TCP?

Monologue: Well, this one, it's very familiar

: Suppose A is the client side and B is the server side.

  1. First, the application process of A sends a connection release segment ( FIN=1,seq=u ) to its TCP, stops sending data again, actively closes the TCP connection, enters the FIN-WAIT-1 (termination waiting 1) state, and waits for B's confirmation.
  2. After B receives the connection release segment, it sends an acknowledgement segment ( ACK=1,ack=u+1,seq=v ), and B enters the CLOSE-WAIT (close waiting) state. At this time, TCP is in a half-closed state, and the connection between A and B is released.
  3. After receiving the confirmation from B, A enters the FIN-WAIT-2 (termination waiting 2) state and waits for the connection release segment sent by B.
  4. After B sends the data, it will send a connection release segment ( FIN=1,ACK=1,seq=w,ack=u+1 ), and B enters the LAST-ACK (final confirmation) state, waiting for A's confirmation.
  5. After A receives the connection release segment from B, it sends an acknowledgement segment ( ACK=1,seq=u+1,ack=w+1 ), and A enters the TIME-WAIT (time waiting) state. At this time, TCP is not released, and it needs to wait for the time set by the timer 2MSL (maximum segment lifetime) before A enters the CLOSED state. B closes the connection after receiving the acknowledgment segment from A. If it does not receive the acknowledgment segment from A, B will retransmit the connection release segment.

Interviewer : handshakes three times when establishing a connection, why does it take four waves to release the connection?

: Because when the connection is established, when the server receives the SYN connection request message from the client, it can directly send SYN+ACK message.

: But when the connection is closed, when the server receives the connection release message sent by the client, it is likely not to close the SOCKET immediately, so the server first replies with a ACK message to tell the client I have received your connection release message. Only after all the packets on the server side have been sent, can the server side send a connection release packet, and then the two sides will truly disconnect. So it takes four waves.

Interviewer : Well, do you understand https? What problem is https for solving?

Monologue: Don't panic at all haha

: HTTP is a plaintext transmission, which is easy to be eavesdropped or tampered with by hackers and is not secure.

: HTTPS mainly solves the defects of the HTTP plaintext protocol, adds the SSL/TLS protocol on the basis of HTTP, relies on the SSL certificate to verify the identity of the server, and establishes an SSL channel between the client and the server to ensure data transmission. Safety.

Interviewer : What is the difference between http and https?

: The difference between http and https is as follows:

  1. HTTP is a hypertext transfer protocol, information is clear text transmission ; HTTPS is an ssl encrypted transfer protocol with security .
  2. HTTP and HTTPS use different ports, HTTP port is 80, HTTPS is 443.
  3. The HTTPS protocol needs to apply for the certificate from the CA agency, which generally requires a certain fee.
  4. HTTP runs on top of TCP protocol; HTTPS runs on top of SSL protocol, and SSL runs on top of TCP protocol.

Interviewer : good, let’s ask about MySQL

Interviewer : what circumstances will the index fail?

: There are several situations that will cause the index to fail.

  • For composite indexes, instead of using the leftmost field of the composite index, the index will not be used
  • Like queries that start with %, such as %abc , cannot use indexes; like queries that do not start with %, such as abc% , are equivalent to range queries and use indexes.
  • The column type in the query condition is a string, and no quotation marks are used. Implicit conversion may occur due to different types, making the index invalid.
  • When judging whether the index column is not equal to a certain value
  • operate on indexed columns
  • The query condition uses or connection, which will also cause the index to fail

Interviewer : Very good, can I join the job tomorrow?

Monologue: A three-piece suit of a vest and helmet box?


程序员大彬
468 声望488 粉丝

非科班转码,个人网站:topjavaer.cn