ThreadLocal(thread variable copy)

Synchronized implements memory sharing, and ThreadLocal maintains a local variable for each thread.

Using space for time, it is used for data isolation between threads, providing a copy for each thread that uses this variable, and each thread can independently change its copy without conflict with the copy of other threads.

The ThreadLocal class maintains a Map that stores copies of variables for each thread. The keys of elements in the Map are thread objects and the values are copies of variables for the corresponding thread.

ThreadLocal plays a huge role in Spring, with its presence in managing beans, transaction management, task scheduling, AOP, and other modules in the Request scope.

Most beans in Spring can be declared Singleton scoped and wrapped with ThreadLocal, so stateful beans can work Singleton style in multithreading.

Links: Delve into the java.lang.ThreadLocal class


Java memory model:

The Java Virtual Machine specification divides Java runtime data into six categories.

1. Program counter: is a data structure that holds the memory address of the currently executing program. Multithreading in the Java VIRTUAL machine is implemented by switching threads in turn and allocating processor time. In order for threads to recover to the correct position after switching, each thread needs an independent program counter, which does not affect each other. This area is “thread private”.

2.Java virtual machine stack: Thread-private, same as thread life cycle, used to store local variable table, operation stack, method return value. The local variable table holds the basic data types, as well as references to objects.

3. Native method stack: Similar to the virtual machine stack, but it serves Native methods used by the virtual machine.

4.Java heap: An area of memory shared by all threads where object instances are almost always allocated memory.

5. Method area: the area shared by each thread, storing the class information loaded by the VIRTUAL machine, constants, static variables, and compiled code.

6. Runtime constant pool: Represents constant tables in each class file at runtime. This includes several constants: compile-time numeric constants, method or field references.

Friendship link: Details of the JVM virtual machine in Java


“Can you talk about when, what, and what the Java GC did?”

When:

1. The Cenozoic generation has one Eden zone and two survivor zones. First, put the object into Eden zone. Then clear Eden and the previous survivor zone. During a GC, if objects are found that are still not fit, they are put into old memory.

2. Large objects and long-lived objects directly enter the old age zone.

3. Every time you perform a MINOR GC, you should analyze the objects that are being promoted to the old age. If the size of the objects that are going to the old age area exceeds the remaining size of the old age area, you should perform a Full GC to get as much space as possible from the old age area.

What to do: Objects that can’t be found from GC Roots and still haven’t been resurrected after a mark sweep.

What to do: New Generation: Copy and clean up; The old days: mark-sweep and mark-compress algorithms; Permanent generation: The classloader itself that holds classes in Java and loads classes.

GC Roots: 1. Objects referenced in the virtual machine stack 2. 3. Objects referenced by JNI (generally known as Native methods) in the local method stack.

The Java GC Thing (part 1)

The Java GC thing (part 2)

Links: INTRODUCTION to the CMS garbage collector


Synchronized and Lock are both reentrant locks, when the same thread re-enters the Synchronized code. You can use the locks you already have.

Synchronized is a pessimistic lock mechanism. Locks.ReentrantLock, on the other hand, is to complete an operation each time without locking, assuming no conflicts, and then retry it until it succeeds if it fails because of conflicts. ReentrantLock Application scenario

  1. A thread needs to interrupt while waiting for control of a lock
  2. The Condition application in ReentrantLock can control which thread of notify. The lock can bind multiple conditions.
  3. With fair locking, each incoming thread will queue up.

Synchronized keyword, Lock, and explain the differences between them


StringBuffer is thread-safe; every time you manipulate a String, String generates a new object, which StringBuffer does not; StringBuilder is non-thread-safe

The difference between String, StringBuffer, and StringBuilder


Fail-fast: Mechanism is a bug mechanism in a Java Collection. Fail-fast events can occur when multiple threads operate on the contents of the same collection. For example, when thread A iterates through A collection, the contents of the collection are changed by other threads. Then thread A access to the collection, will throw ConcurrentModificationException, produce fail – fast


Happens-before: If two operations have a happens-before relationship, the result of the previous action is visible to the later action. 1. Program order rule: Every action in a thread happens- before any subsequent action in that thread. 2. Monitor lock rule: A monitor lock is unlocked, happens-before the monitor lock is subsequently locked. 3. Volatile variable rules: Writes to a volatile field are happens-before any subsequent reads to that volatile field. 4. If A happens-before B, and B happens-before C, then A happens-before C. 5. Thread start rule: The Thread object’s start() method happens-before every action of the Thread.


Four differences between Volatile and Synchronized: 2 SYN blocks, volatile threads do not block. 3 SYN guarantees three properties. Volatile does not guarantee atomicity. 4 SYN compiler optimization. Ensure that the variable is visible to all threads. When one thread changes the value of the variable, the new value is visible to other threads, but is not multithreaded safe. 2. Disable instruction reordering optimization. How Volatile ensures memory visibility: 1. When a Volatile variable is written, the JMM flusher the shared variable from the thread’s local memory to main memory. 2. When a volatile variable is read, the JMM invalidates the thread’s local memory. The thread will next read the shared variable from main memory.

Synchronization: The completion of one task depends on another task. The dependent task can be completed only after the dependent task is completed. Asynchronous: there is no need to wait for the dependent task to complete, but only notify the dependent task to complete what work, as long as the completion of their own task is considered complete, the dependent task will be notified back whether completed. Asynchrony is characterized by notification. Phone calls and text messages are used as metaphors for synchronous and asynchronous operations. Block: The CPU stops and waits for a slow operation to complete before continuing to do other work. Non-blocking: Non-blocking is when the CPU does something else while the slow execution is complete, and then the CPU completes the next operation. Non-blocking leads to an increase in thread switching, and it needs to be considered whether the increased CPU usage time can compensate for the system switching costs.

Volatile keyword parsing for Java concurrent programming


Compare And Swap (CAS) Lock-free algorithm: CAS is an optimistic lock technology. When multiple threads try to update a variable using CAS, only one thread can update the value of the variable. However, all other threads fail. CAS has three operands, the memory value V, the old expected value A, and the new value B to modify. Change the memory value V to B if and only if the expected value A and memory value V are the same, otherwise nothing is done.

Friendship link: Non-blocking synchronization algorithm and CAS(Compare and Swap) lock-free algorithm


The purpose of a thread pool is to create a number of threads to respond to processing when the program is started. These threads are called thread pools. The threads in these pools are called worker threads. Reduce the cost of thread creation and destruction by reusing created threads. Second: improve response speed. When a task arrives, it can be executed immediately without waiting for the thread to be created. Third: improve thread manageability. Common thread pools: ExecutorService is the main implementation class, The Executors. The commonly used ones are newSingleThreadPool (), newFixedThreadPool (), newcachedTheadPool (), newScheduledThreadPool ().

Link: Thread pool principle

Thread pool principle analysis


1. Load: The Java binary code is imported into the JVM to generate a Class file. A) verify: check the correctness of the data loaded into the Class file b) Prepare: allocate storage space to static variables c) parse: convert symbolic references to direct references 3) Initialize: initialize static variables, static methods, and static code blocks of the Class.

Parental delegation model: when the classloader receives a classloading request, it first delegates the request to the parent classloader to complete the user-defined loader -> application loader -> extension classloader -> start classloader.

Links: In-depth understanding of the Java Virtual Machine Note-parent Delegation model

Friendship links: Things about JVM class loading


Consistency hashing:

Memcahed Cache: Data structure: key,value Pair Usage methods: get,put, etc

The hashcode(),equal() methods are explored in depth


Redis data structure: Redis’s Hash structure allows you to modify only one property value as if updating a property in a database. List – List implements message queue Set – Collection uses unique Sorted data Set – Ordered collections can be sorted to enable data persistence

Spring + Redis implements data caching


Java automatic packing and unpacking in-depth analysis

Talk about Java reflection

How do I write an immutable class?


Index: B+, B-, full-text index MySQL’s index is a data structure designed to enable the database to find data efficiently. The commonly used data structure is B+Tree. Each leaf node not only stores the relevant information of the index key but also adds Pointers to adjacent leaf nodes, thus forming a B+Tree with sequential access Pointers. The purpose of this optimization is to improve the performance of different interval access. When to use indexes: 1. Fields that often appear after the group by, Order by, and distinc keywords 2. Tables that are frequently joined with other tables should have an index on the join field. Fields that are used as query choices often appear

MySQL: InnoDB storage engine B+ tree index algorithm

MySQL index data structure and algorithm principle


Spring IOC (Inversion of Control, Dependency Injection)

Spring supports three dependency injection methods: property (Setter) injection, construct injection, and interface injection.

In Spring, the bodies that make up the application and the objects managed by the Spring IOC container are called beans.

Spring’s IOC container instantiates beans and establishes dependencies between beans through a reflective mechanism. Simply put, beans are objects that are initialized, assembled, and managed by the Spring IOC container. The process of getting a Bean object first loads the configuration file through Resource and starts the IOC container, then gets the Bean object through the getBean method and calls his method. Spring Bean scope: Singleton: There is only one shared Bean instance in the Spring IOC container, which is typically Singleton scoped. Prototype: For each request, a new Bean instance is generated. Request: Each HTTP Request generates a new Bean instance.

The Spring framework IOC container and AOP parsing

A brief analysis of the usage of Spring Framework annotations

Links: 69 Interview Q&a’s about Spring — the Ultimate List


The common advantage of the proxy is that the business class only needs to focus on the business logic itself, ensuring the reuse of the business class. Java static proxy: The proxy object implements the same interface as the target object. The target object is an attribute of the proxy object. In the implementation of the interface, the proxy object can add other business processing logic before and after calling the corresponding methods of the target object. Disadvantages: A proxy class can only represent one business class. If the business class adds methods, the corresponding proxy class also adds methods. Java dynamic proxy: Java dynamic proxy is to write a class to implement the InvocationHandler interface and rewrite the Invoke method. The Invoke method can be used to write logic for enhanced processing. This public proxy class can specify the object it wants to proxy when running, and realize the implementation of the method of the proxy class. Enhancements can then be made when class methods are implemented. In effect: methods of the proxy object = enhanced processing + methods of the proxied object

The difference between JDK and CGLIB: JDK dynamic proxies can only generate proxies (instantiate a class) for classes that implement interfaces. The proxy object and target object implements the same interface, the target object as a property of a proxy object, the concrete interface implementations, you can invoke the target object in corresponding method combined with other business processing logic before and after additional for class implements agents, mainly to the specified class to generate a subclass (not instantiate a class), covering the method. Spring AOP application scenarios performance testing, access control, log management, transactions, etc. The default strategy is to use JDK dynamic proxy technology if the target class implements the interface, and CGLIB proxy if the target object does not


1. Client request submitted to DispatcherServlet 2. The DispatcherServlet Controller queries the HandlerMapping and dispatches it to the specified Controller. 5. DispatcherServlet queries one or more ViewResoler view parsers and finds the view specified by ModelAndView 6. The view is responsible for displaying the results to the client

Spring: Annotation-based Spring MVC (Part 1)

Spring: Annotation-based Spring MVC (Part 2)

Summary of differences and comparisons between SpringMVC and Struts2

SpringMVC vs. Struts2


An Http request DNS domain name resolution – > initiates a TCP three-way handshake – > initiates an Http request after establishing a TCP connection – > The server responds to the Http request and the browser gets the HTML code – > the browser parses the HTML code, And request resources in HTML code (such as JS, CSS, images, etc.) – > browser to render the page to the user

Design to store huge amounts of data storage systems: one is called “middle tier” of a logic layer, in this layer, the database of huge amounts of data out, make the cache, running on the server’s memory, in the same way, when you have the new arrival of the data, make the cache first, again to find a way to, persisted in the database, this is a simple way of thinking. The main step is load balancing, which distributes the requests from different users to different processing nodes, and then stores them in the cache and periodically updates the data to the main database. The read/write process adopts a similar mechanism of optimistic locking, which can keep reading (even when writing data), but each time the read will be marked with a version. If the read version is lower than the cached version, the data will be re-read, which is rare and tolerable.

The difference between HTTP and HTTPS

Why is HTTPS more secure

Friendship links: HTTP request packets and HTTP response packets

HTTP request mode: comparison of GET and POST


The Session with the cookies: Cookies can make the server track each client’s access, but each client’s access must return these cookies, if there are many cookies, it will intangibly increase the amount of data transmission between the client and the server, and Session is a good solution to this problem, each time the same client interacts with the server, To store data to the server through a Session, you don’t need to return all the Cookie values each time. Instead, you need to return an ID, a unique ID generated by each client when accessing the server for the first time. The client simply returns this ID. So the server can use this ID to retrieve the KV value stored in the server. Session and Cookie timeouts, and Cookie security


Distributed Session framework 1. Configure servers. The Zookeeper cluster management server can centrally manage configuration files of all servers. These shared sessions are stored in a distributed cache, which can be written and read at any time, and have good performance, such as Memcache, Tair. Encapsulate a class that inherits from HttpSession, store sessions in this class, and store sessions in the distributed cache. Because cookies cannot be accessed across domains, you need to synchronize Session ids to different domain names to achieve Session synchronization.


Adapter pattern: ADAPTS one interface to another. Java I/O InputStreamReader ADAPTS the Reader class to InputStream, which implements the quasi-conversion of byte streams to character streams. Decorator mode: Keep the original interface and enhance the original functionality. FileInputStream implements all the interfaces of InputStream. BufferedInputStreams inherits from FileInputStream, which is the concrete decorator implementer that stores the content read by InputStream in memory to improve read performance.


Spring transaction configuration methods: 1. Pointcut information, the business class method used to locate the implementation of the transaction aspect 2. Transaction properties that control transaction behavior, including transaction isolation level, transaction propagation behavior, timeout, and rollback rules. Spring uses the AOP/TX Schema namespace and the @Transaction annotation technique for declarative Transaction configuration.


Mybatis Every Mybatis application has an instance of the SqlSessionFactory object as its core. First read the configuration file through Resource using the byte stream. Then create SqlSessionFactory using the SqlSessionFactoryBuilder().build method. Then through sqlSessionFactory. OpenSession () method to create a sqlSession for each database transaction service. Experienced Mybatis initialization – > create SqlSession – > run SQL statement to return results of three processes


The difference between Servlet and Filter: The whole process is: The Filter preprocesses the user request, then sends the request to the Servlet for processing and generates the response, and finally the Filter postprocesses the server response.

The functions of Filter are as follows: Filter can perform pre-processing and post-processing for specific URL requests. Intercepts the client’s HttpServletRequest until it reaches the Servlet. Check the HttpServletRequest as needed, and modify the HttpServletRequest header and data as well. Intercepting HttpServletResponse before it reaches the client. Check the HttpServletResponse as needed, and modify the HttpServletResponse headers and data as well.

In fact, a Filter is very similar to a Servlet, except that a Filter cannot directly generate a response to the user. In fact, the code in the doFilter() method of Filter is generic code extracted from the service() method of multiple servlets for better reuse.

The lifecycle of the Filter and Servlet is as follows: 1. The Filter is initialized when the Web server starts 2. If a Servlet is configured with 1, the Servlet is also initialized when Tomcat (Servlet container) starts. 3. If the Servlet is not configured 1, the Servlet will not be initialized when Tomcat starts, but when the request arrives. 4. Each time a Request is made, the Request is initialized and destroyed. 5. After the Servlet is initialized, it will not be logged out as the request ends. 6. When Tomcat is closed, the Servlet and Filter are logged out in sequence.


Differences between HashMap and HashTable. 1. HashMap is thread-safe, HashTable is thread-safe. 2. A HashMap allows null values for both keys and values, whereas a HashTable does not. 3. Because of thread-safety issues, HashMap is more efficient than HashTable.

1. Maintain a data structure in which each element is a linked list and each node in the list is an Entry[] key-value pair. 2. Realized the array + linked list features, fast search, insertion and deletion is also fast. 3. For each key, its array index index is int I = hash(key.hashCode)&(len-1); 4. Each newly added node is placed at the beginning of the linked list, and the newly added node points to the beginning of the original linked list

HashMap is different from TreeMap

Link: Understand the difference between HashMap and TreeMap in Java

A HashMap conflict

HashMap conflict resolution method and principle analysis

Link: How HashMap works

The difference between HashMap and Hashtable

There are two ways to make the HashMap thread safe


Differences between HashMap, ConcurrentHashMap and LinkedHashMap

  1. ConcurrentHashMap uses lock fragmentation technology to ensure thread-safety. The lock fragmentation technology divides data into segments and assigns a lock to each segment of data. When a thread accesses one segment of data, the other segments of data can be accessed by other threads
  2. ConcurrentHashMap is thread-safe in each segment
  3. LinkedHashMap maintains a double-linked list of data that can be read out in the order in which it is written

ConcurrentHashMap Application scenario

1: ConcurrentHashMap is used in high concurrency scenarios, but it does not guarantee thread-safety, whereas synchronized HashMap and HashMap lock the entire container, ConcurrentHashMap does not need to lock the entire container, but only the corresponding Segment. Therefore, high concurrent and synchronous access can be guaranteed, which improves efficiency.

2: can be multithreaded write. ConcurrentHashMap Divides the HashMap into several Segmenet 1. When get, it does not lock, locate the segment first and then read the segment after finding the header. Value is volatile, so it is guaranteed that the latest value is read during the race condition. If the value is null, it may be changing, so ReadValueUnderLock is called to ensure that the read data is correct. 2. When Put, the lock is added to the head of the hash chain. 3.Remove will also be locked. As next is final and cannot be changed, all nodes before the deleted node must be copied. 4.ConcurrentHashMap allows multiple change operations to be performed concurrently. The key is the use of lock separation technology. It uses multiple locks to control changes to different segments of the Hash table.

ConcurrentHashMap is used in high concurrency scenarios, but it does not guarantee thread-safety. A synchronized HashMap or HashTable locks the entire container, while a ConcurrentHashMap does not need to lock the entire container, but only the corresponding segment. Therefore, high concurrent and synchronous access can be guaranteed, which improves efficiency.

Java Collection – ConcurrentHashMap principle analysis


Difference between Vector and ArrayList

The difference between Vector and ArrayList in Java


The ExecutorService service = Executors… . ExecutorService service = new ThreadPoolExecutor() ExecutorService service = new ScheduledThreadPoolExecutor();

ThreadPoolExecutor source code analysis

State of the thread pool itself:

Waiting task queues and working sets:

Key state locks for thread pools:

Thread pool lifetime and size:

With the data defined above, let’s look at how ThreadPoolExecutor works internally. If the current poolSize poolSize is smaller than the corePoolSize, create a new thread to execute the task. 2. If the current poolSize poolSize is greater than corePoolSize and the queue is not full, the queue is entered. 3. If the current poolSize poolSize is greater than corePoolSize and smaller than maximumPoolSize, and the wait queue is full, a new thread is created to execute the task. 4. If the current poolSize poolSize is larger than corePoolSize and larger than maximumPoolSize, and the wait queue is full, the reject policy is invoked to process the task. 5. Each thread in the thread pool does not exit immediately after completing a task. Instead, it checks to see if there are any other threads in the queue that need to be executed.

Executor package structure

CopyOnWriteArrayList : Write lock, when adding an element, the original container to copy and replicate a new container, and then write in the new container, written after the original container reference point to new container, and read data is read old container, so can be concurrent read, but it is a kind of weak consistency strategy. Usage scenarios: CopyOnWriteArrayList is suitable for scenarios where reads are much larger than writes, such as caching.


Linux common commands: CD, cp, mv, rm, ps (process), tar, cat(view content), chmod, vim, find, ls


Prerequisites for deadlocks 1. Mutually exclusive At least one resource is in the unshared state 2. Hold and wait 3. Non-preemption 4. Cycle and wait to resolve deadlocks. The first is deadlock prevention, which does not allow the above four conditions to hold at the same time. Second, rational allocation of resources. The third is to use the banker algorithm, if the process request resources operating system surplus can meet, then allocate.


Communication between processes

  1. Pipe: A pipe is a half-duplex communication mode in which data flows only in one direction and can only be used between related processes. Process kinship usually refers to the parent-child process relationship.
  2. Named pipe: Named pipe is also a half-duplex communication mode, but it allows communication between unrelated processes. 3. Semophore: A semophore is a counter that can be used to control access to a shared resource by multiple processes. It is often used as a locking mechanism to prevent other processes from accessing a shared resource while one process is accessing it. Therefore, it is mainly used as a means of synchronization between processes and between different threads within the same process.
  3. Message queue: MESSAGE queues are linked lists of messages stored in the kernel and identified by message queue identifiers. The message queue overcomes the disadvantages of little signal transmission, pipe carrying only plain byte stream and limited buffer size. 5. Sinal: Signals are a complex form of communication used to inform the receiving process that an event has occurred. 6. Shared memory: A shared memory map maps a segment of memory that can be accessed by other processes. This shared memory is created by one process but can be accessed by multiple processes. Shared memory is the fastest IPC method and is specifically designed for the low efficiency of other interprocess communication methods. It is often used in conjunction with other communication mechanisms, such as semaphores, to achieve synchronization and communication between processes. 7. Socket: Socket is also an interprocess communication mechanism. Different from other communication mechanisms, it can be used for process communication between different machines.

The difference and relation between process and thread

Operating system process scheduling algorithm

A detailed explanation of the hierarchical storage structure of computer systems


A database transaction is a series of operations performed as a single logical unit of work.

Friendship links: Four features of database transactions and transaction isolation levels


MySQL database optimization summary

MYSQL optimization common methods

MySQL storage engine -MyISAM is different from InnoDB

About patterns in SQL databases


Hibernate’s level 1 cache is provided by the Session, so it only exists during the lifetime of the Session. When the program calls the save(),update(),saveOrUpdate() methods and calls the query interface list,filter,iterate, etc., If an object does not already exist in the Session cache, Hibernate adds the object to the level 1 cache and the cache disappears when the Session is closed. Hibernate level 1 cache is built into Session and cannot be unloaded or configured in any way. Level 1 cache is implemented in the key-value Map mode. When caching entity objects, the primary key ID of the object is the key of the Map, and the entity object is the corresponding value.

Hibernate level 2 cache: Puts all obtained data objects into the level 2 cache based on their IDS. Hibernate level 2 cache policy is a cache policy for ID queries, which updates the cache when deleting, updating, and adding data.


Various Java interface resources

An Interview for a Java engineer

An Interview for a Java engineer

Java Interview Reference Guide (1)

Java Interview Reference Guide (2)

Ali comes back from an interview and wants to talk to a Java programmer

Interview experience and summary — BAT, netease, Mushroom Street

2017 Xiaomi Spring recruitment internal push interview