Joke Collection Website - Public benefit messages - What are the concurrency frameworks in Java web and what are they?

What are the concurrency frameworks in Java web and what are they?

1. Concurrency is a requirement. Let’s first introduce Javaweb’s ideas for handling high concurrency:

1. The synchronized keyword

can be used to give objects Locking with methods or code blocks. When it locks a method or a code block, at most one thread can execute this code at the same time. Possible lock objects include: this, critical resource object, Class object

2. Synchronization method

The synchronization method locks the current object. When multiple threads call the current synchronized method multiple times through the same object reference, they need to be executed synchronously.

3. Synchronized code blocks

The synchronization granularity of synchronized code blocks is more detailed and is the recommended programming method in commercial development. You can locate the specific synchronization location, rather than simply implementing the synchronization logic as a whole. In terms of efficiency, it is relatively higher.

A) Lock critical objects

When the synchronized code block is executed, the object object is locked. When multiple threads call the same method, they need to be executed synchronously if the locked object remains unchanged.

B) Lock the current object

4. The underlying implementation of the lock

Synchronization (Synchronization) in the Java virtual machine is based on entering and exiting the monitor (Monitor) Object implementation. The synchronization method is not implemented by the monitor enter and monitor exit instructions, but is implemented implicitly by the method call instruction to read the ACC_SYNCHRONIZED flag of the method in the runtime constant pool.

5. Types of locks

The types of locks in Java are roughly divided into bias locks, spin locks, lightweight locks, and heavyweight locks.

The way to use locks is: first provide biased locks, if not satisfied, upgrade to lightweight locks, and if not satisfied again, upgrade to heavyweight locks. A spin lock is a transitional lock state, not an actual lock type.

Locks can only be upgraded, not downgraded.

6. Volatile keyword

Thread visibility of variables. During the CPU calculation process, the data required for the calculation process will be loaded into the CPU calculation cache. When the CPU calculation is interrupted, it is possible to refresh the cache and re-read the data in the memory. During the thread running process, if a variable is modified by other threads, data inconsistency may occur, resulting in incorrect results. The volatile-modified variables are visible to the thread. When the JVM interprets the volatile-modified variables, it will notify the CPU. During the calculation process, every time a variable is used to participate in the calculation, it will check whether the data in the memory has changed, instead of always using it. The data in the CPU cache can ensure the accuracy of calculation results.

More, there are many details that need to be understood and improved through study, so I won’t list them all here.

2. Concurrency Framework

There are many concurrency frameworks, such as ExecutorService, RxJava, Disruptor, Akka, etc. The specific choice (or none) is based on the project requirements, and the framework itself The difference is not big, they are basically the following patterns