GitHub 6.6K Star Java engineer into god road, don’t come to know?
GitHub 6.6K Star Java engineer into the road to god, really don’t come to know?
GitHub 6.6K Star Java engineer into the road to god, really sure not to understand it?
This is a zhihu above a fire problem (www.zhihu.com/question/50)… Here’s my answer to that question, which has garnered 500+ likes and 70+ comments as of today.
The original answer
It’s just a question of getting warmed up.
What’s the difference between StringBuffer and StringBuilder?
What is thread-safe?
How do I ensure thread safety?
What is a lock? A deadlock?
What is the implementation of synchronized?
What’s the point of volatile when you have synchronized?
What about lock optimization with Synchronized? Lock coarsening? Lock elimination? The spin lock? Biased locking? Lightweight locks?)
Do you know JMM? (Atomicity? Visibility? Order?)
Java and send packages understand?
So what is fail-fast? What is fail-safe?
What is CopyOnWrite?
The AQS? The CAS?
CAS knows, so optimistic lock must know, right?
What is the difference between optimistic lock and pessimistic lock?
How to implement pessimistic and optimistic locking?
What about database locks? Row-level locks? Table level lock? A Shared lock? Exclusive lock? Gap lock? Next, the key lock?
What does database locking have to do with isolation levels?
What is the relationship between database locks and indexes?
What is a cluster index? Non-clustered index? What’s the leftmost prefix? B+ tree index? Federated index? Back to the table?
Distributed locking?
How does Redis implement distributed locking?
Why use Redis?
What is the difference between Redis and memcache?
How does Zookeeper implement distributed locks?
What is Zookeeper?
What is CAP?
What is BASE? What’s the difference with CAP?
How do we derive CAP? How to choose?
How can distributed systems ensure data consistency?
What is a distributed transaction? Distributed transaction solution?
So, finally, how about hand-writing a thread-safe singleton?
Can we implement thread-safe singletons without synchronized and lock?
You can answer all that? Okay, can you explain to me what the Paxos algorithm is?
He ~!
Part of the answer
In view of the above questions, I give some answers, I hope you can find something. These answers are also drawn from historical posts on my blog.
Part of the questions can be said in a few simple words, I directly posted the answer, more complex, I posted a portal.
However, the following answers may not be complete, such as some comparative ones, I may only say the key points or the key points related to the body of knowledge, so I hope the reader can improve the answer to each question according to the following questions.
What’s the difference between StringBuffer and StringBuilder?
A StringBuffer is thread-safe while a StringBuilder is non-thread-safe.
What is thread-safe?
Thread safety is a programming term, refers to a function, function library is called in a concurrent environment, can correctly handle the shared variables between multiple threads, so that the program function to complete correctly. That is, in multithreaded scenarios, ordering, atomicity and visibility problems do not occur.
How do I ensure thread safety?
Java mainly through locking to achieve thread safety. Synchronized and Lock are usually used
What is a lock? A deadlock?
A deadlock is a blockage caused by two or more processes competing for resources or communicating with each other during execution, so that they cannot proceed without external forces. At this point, the system is said to be in a deadlock state or the system has produced a deadlock, and these processes that are always waiting for each other are called deadlock processes.
For a deadlock to occur, the following four conditions must be met: mutual exclusion condition, request and hold condition, non-deprivation condition, and loop wait condition
The solution to a deadlock is to break one or more of the above four prerequisites.
What is the implementation of synchronized?
Get a deeper understanding of Multi-threading (1) — Implementation of Synchronized (4) — Implementation of Moniter Send this article to anyone who asks you what Synchronized is
What’s the point of volatile when you have synchronized?
Often referred to as “lightweight synchronized,” volatile guarantees visibility and order through memory barriers.
Volatile has an important function that synchronized does not: it disallows reordering instructions. This feature is useful when implementing singletons with double-check locks, which use the synchronized keyword, but can be problematic if singletons are not volatile.
The next time anyone asks you what synchronized is, send this article to them.
What about lock optimization with Synchronized? Lock coarsening? Lock elimination? The spin lock? Biased locking? Lightweight locks?)
In-depth understanding of multithreading (5) — Lock optimization techniques for the Java Virtual Machine
Do you know JMM? (Atomicity? Visibility? Order?)
Java Memory Model (JMM) is a mechanism and specification that conforms to the Memory Model specification, shields the access differences of various hardware and operating systems, and ensures the consistent effect of Java programs accessing Memory on various platforms.
Send this article to anyone who asks you what the Java memory model is.
Java and send packages understand?
The java.util.Concurrent package (J.U.C) contains some useful utility classes for concurrent programming in Java, including several sections:
Part 1, the locks: included in Java. Util. Concurrent. The locks in the package, provide explicit lock (lock mutex and sketch) related functions;
Part 2, atomic: included in Java. Util. Concurrent. The atomic package, provide the atomic variable classes related functions, is the foundation of building a nonblocking algorithms;
Executor section: Scattered in the java.util.concurrent package, providing thread pool-related functionality;
Collections: Scattered in the java.util.Concurrent package, which provides concurrent container functionality;
Tools: Scattered in the java.util.concurrent package, providing synchronization tool classes such as semaphores, locking, fences, and so on;
So what is fail-fast? What is fail-safe?
By default, when we say fail-fast in Java, we mean an error detection mechanism for Java collections. When multiple threads to the operation of the part of the change in the structure of the collection, is likely to fail – fast mechanism, this time will throw ConcurrentModificationException.
ConcurrentModificationException, when concurrent modification method detected object, but does not allow this change when you throw the exception.
To avoid triggering fail-fast and causing an exception, we can use some of the fail-safe collection classes provided in Java.
Instead of directly accessing the contents of the collection, such a collection container first copies the contents of the original collection and traverses the copied collection.
Containers under the java.util.concurrent package are fail-safe and can be used and modified concurrently in multiple threads. You can also add/remove in foreach.
What the hell is fail-fast?
What is CopyOnWrite?
Copy-on-write, COW for short, is an optimization strategy used in programming. The basic idea is that everyone shares the same content from the beginning, and only when someone wants to modify the content will the content be copied out to form a new content and then change it, which is a kind of lazy delay strategy.
The CopyOnWrite container is a container that is copied on write. When we add elements to a container, we do not add them directly to the current container. Instead, we Copy the current container to create a new container, and then add elements to the new container. After adding elements, we reference the original container to the new container.
What the hell is fail-fast?
The AQS? The CAS?
AQS (AbstractQueuedSynchronizer), that is, the queue synchronizer. It is the basic framework for building locks or other synchronization components (such as ReentrantLock, ReentrantReadWriteLock, Semaphore, etc.), and the author of JUC and sending packages (Doug Lea) expects it to be the basis for implementing most of the synchronization requirements. It is the core foundation component in JUC and sending packages.
CAS is an optimistic locking technique. When multiple threads attempt to update the same variable using CAS, only one of them can update the value of the variable while the others fail. The failed thread is not suspended, but is told that it failed in the race and can try again.
The CAS operation contains three operands — memory location (V), expected original value (A), and new value (B). If the value of the memory location matches the expected original value, the processor automatically updates the location value to the new value. Otherwise, the processor does nothing. In either case, it returns the value for that position before the CAS directive. (Some special cases with CAS will simply return whether the CAS was successful, without extracting the current value.) CAS effectively says “I think position V should contain the value A; If this value is included, put B in this position; Otherwise, don’t change the location, just tell me what the location is now.” This is actually the same principle as the optimistic lock conflict check + data update.
One implementation of optimistic locking is CAS
CAS knows, so optimistic lock must know, right?
Optimistic Locking Compared with pessimistic Locking, Optimistic Locking assumes that data does not cause conflicts in general. Therefore, it checks whether data conflicts are found when data is submitted for update. If a conflict is found, error information is returned to the user and the user can decide what to do.
In contrast to pessimistic locks, optimistic locks do not use the locking mechanism provided by the database when the database is being processed. A common way to implement optimistic locking is to record data versions.
There are two ways to implement data versioning, the first using version numbers and the second using timestamps.
Deep understanding of optimistic lock and pessimistic lock
What is the difference between optimistic lock and pessimistic lock?
Same as above
How to implement pessimistic and optimistic locking?
Deep understanding of optimistic lock and pessimistic lock
What about database locks? Row-level locks? Table level lock? A Shared lock? Exclusive lock? Gap lock? Next, the key lock?
MySQL – Row level lock, table level lock, page level lock
Shared and exclusive locks in MySQL
What does database locking have to do with isolation levels?
Many DBMSS define different “transaction isolation levels” to control the degree of locking and concurrency.
There are four standard isolation levels defined by ANSI/ISO SQL, which are Serializable, Repeatable reads, Read Committed, and Read Uncommitted.
Delve into the isolation level of the transaction
What is the relationship between database locks and indexes?
In MySQL, row-level locking does not directly lock records, but rather locks indexes. There are two types of indexes: primary key index and non-primary key index. If an SQL statement operates on a primary key index, MySQL will lock the primary key index. If a statement operates on a non-primary key index, MySQL will first lock the non-primary key index and then lock the related primary key index.
What is a cluster index? Non-clustered index? What’s the leftmost prefix? B+ tree index? Federated index? Back to the table?
The leaf node of the primary key index stores the entire row. In InnoDB, primary key indexes are also called clustered indexes.
In InnoDB, a non-primary key index is also called a non-clustered index (secondary index).
When we create a union index, such as (key1,key2,key3), we create (key1), (key1,key2), and (key1,key2,key3). This is the leftmost matching principle.
In InnoDB, the leaf of index B+ Tree is the primary key index that stores the entire row. The leaf of index B+ Tree is a non-primary key index that stores the primary key value. Because the primary key index tree leaf node is directly the whole row of data we want to query. The leaf node that is not the primary key index is the value of the primary key. After the value of the primary key is found, it needs to be queried again through the value of the primary key. This process is called backtable.
I thought I knew a lot about Mysql indexing until I met ali’s interviewer
Distributed locking?
At present, the most commonly used schemes are as follows:
Distributed lock based on database based on cache (Redis, memcached, TAIR) to achieve distributed lock based on Zookeeper to achieve distributed lock
Distributed lock several implementations ~
How does Redis implement distributed locking?
Multiple processes execute the following Redis commands:
SETNX lock.foo
If SETNX returns 1, the process acquired the lock, and SETNX sets the value of the key lock.foo to the timeout period of the lock (current time + the validity period of the lock). If SETNX returns 0, another process has acquired the lock and cannot enter the critical section. A process can repeatedly attempt SETNX operations in a loop to acquire the lock.
Why use Redis?
Distributed cache to improve performance
What is the difference between Redis and memcache?
1. Storage mode: Memcache stores all data in the memory, and it will fail after power failure. Data cannot exceed the memory size. Redis supports data persistence. Data stored in memory can be saved to disk, which can be reloaded and used upon restart. (RDB snapshots and AOF logs can be persisted.)
2. Redis supports data backup and master-slave data backup.
3. Data support type: Redis supports much more data than Memcache.
4. Using the underlying model is different: the new version of Redis builds the VM mechanism itself directly, because the general system calls system functions, will waste a certain amount of time to move and request.
How does Zookeeper implement distributed locks?
Distributed locking based on temporary ordered nodes in ZooKeeper.
The general idea is that when each client locks a method, a unique instantaneous ordered node is generated in the directory of the specified node corresponding to the method on ZooKeeper. The way to determine whether to acquire the lock is very simple, only need to determine the sequence number of the smallest node. When the lock is released, simply delete the transient node. At the same time, it can avoid the service downtime caused by the lock cannot be released, and the deadlock problem.
Distributed lock several implementations ~
What is Zookeeper?
Zookeeper is an open source distributed service coordination component that is an open source implementation of Google Chubby. Is a high performance distributed data consistency solution. It encapsulates complex, error-prone distributed consistency services into an efficient and reliable set of primitives and provides a series of easy-to-use interfaces for users to use.
Zookeeper (2) — Overview of Zookeeper
What is CAP?
CAP theory: A distributed system can only satisfy two of the three items of Consistency, Availability and Partition tolerance at most.
The CAP theory of distributed systems
What is BASE? What’s the difference with CAP?
BASE theory is an extension of CAP theory. The core idea is that even if Strong Consistency cannot be achieved, Eventual Consitency can be achieved in a suitable way.
BASE is Basically Available, Soft State, and Eventual Consistency.
BASE theory of distributed systems
How do we derive CAP? How to choose?
For situations where money is involved and there can be no compromise, C must guarantee. If the network fails, the service should be stopped, which is to guarantee CP and abandon A. For example, in the case of alipay fiber optic cable cut a few years ago, when the network broke down, Alipay chose data consistency between availability and data consistency. What the users felt was that the Alipay system was down for a long time, but actually there were countless engineers behind it to recover data and ensure the consistency of data.
For other scenarios, it is common to choose availability and partition fault tolerance over strong consistency and use final consistency to ensure data security.
The CAP theory of distributed systems
How can distributed systems ensure data consistency?
Distributed transaction
What is a distributed transaction? Distributed transaction solution?
A distributed transaction is one that involves operating on multiple databases. In effect, it extends the concept of transactions for the same library to transactions for multiple libraries. The purpose is to ensure data consistency in distributed systems. The key to distributed transaction processing is that there must be a way to know all the actions the transaction has taken anywhere, and the decision to commit or roll back the transaction must produce uniform results (all commit or all roll back)
About distributed transaction, two phase commit protocol, three phase commit protocol
Distributed transaction solutions — Flexible transaction and Service patterns
So, finally, how about hand-writing a thread-safe singleton?
Seven ways to write the singleton pattern
Why do I recommend using enumerations to implement singletons
Can we implement thread-safe singletons without synchronized and lock?
Implement singleton pattern with CAS (AtomicReference) :
public class Singleton { private static final AtomicReference<Singleton> INSTANCE = new AtomicReference<Singleton>(); private Singleton() {} public static Singleton getInstance() { for (;;) { Singleton singleton = INSTANCE.get(); if (null ! = singleton) { return singleton; } singleton = new Singleton(); if (INSTANCE.compareAndSet(null, singleton)) { return singleton; }}}}Copy the code
The advantage of CAS is that it does not need to use traditional locking mechanism to ensure thread safety. CAS is a busy-wait based algorithm, which depends on the implementation of the underlying hardware. Compared with lock, IT does not have the extra consumption of thread switching and blocking, and can support a large degree of parallelism. An important disadvantage of CAS is the high execution overhead on the CPU if the busy wait keeps executing unsuccessfully (in an infinite loop).
How do I implement a thread-safe singleton without using synchronized and lock?
How do I implement a thread-safe singleton without using synchronized and lock? (2)
You can answer all that? Okay, can you explain to me what the Paxos algorithm is?
Paxos is a consistency algorithm based on message passing with high fault tolerance. Paxos algorithm claims to be the most difficult algorithm to understand!!
conclusion
Interview, in fact, is a step by step process, the interviewer is impossible to let an interviewer hand hand paxOS algorithm, always want to first throw a relatively simple question, and then according to the interview answer, gradually expand and in-depth.
In addition, the “push” process of the above questions is actually a complete knowledge system. Many people in the background of my official account and wechat friends asked me what is the knowledge system and how to build their own knowledge system.
This question does not have what standard answer, same knowledge point, unceasing expansion, connect many knowledge point each other, this is a knowledge system. Everyone’s knowledge system is different. But the build process is the same, which is the depth-first search or the breadth-first search in graph theory, depending on which is right for you.