Concurrent programming synchronized
What I said earlier
As I said at the beginning, I want to sort out some learning documents of java Concurrent Programming. This is the third article: synchronized keyword.
It mainly discusses the use of synchronized keywords, lock principle, lock reentry, dirty reading, lock upgrade and other issues.
1, Introduction to Unsafe class
If you have seen the source code of JUC, I believe you will not be unfamiliar with the Unsafe class. The bottom core of the whole JUC is the Unsafe class. The Unsafe class is located in sun A class under misc package, which is a tool class (underlying c + + implementation) provided by jdk to directly access oper ...
Posted by juliston on Tue, 08 Mar 2022 03:18:42 +0100
1.1 review synchronized
1.2 Lock interface
1.3 Lock method
1.4 difference between the two
1.1 review synchronized
It is a keyword of Java. It is a kind of synchronous lock, which can modify an object, variable and method,
To control this modified, sequential access,
Multithreaded programming steps:
First: create a resource class, at ...
Posted by roice on Mon, 07 Mar 2022 04:39:50 +0100
Example: create a window to sell tickets, and the total number of tickets is 100 Use the way to implement the Runnable interface *
1. Problem: in the process of selling tickets, there are duplicate tickets and wrong tickets -- > there are thread safety problems2. Cause of the problem: when a thread is operating the ticket ...
Posted by hardyvoje on Fri, 04 Mar 2022 23:41:42 +0100
1. Custom thread pool
Thread is a kind of system resource. Every time it is created, it will consume some system resources. If a new thread is created for each task in a high concurrency scenario, it will occupy a lot of system space and even cause the problem of memory overflow. Moreover, the number of threads cannot be too large. If there ...
Posted by Hoangsta on Mon, 14 Feb 2022 02:04:24 +0100
Scene description Briefly describe this step. Open three threads and call three different methods. Use the countdowncatch counter to wait for the completion of all three methods, and then merge the data
introduce CountDownLatch is a synchronization tool class that allows one or more threads to wait until other t ...
Posted by davidjwest on Thu, 10 Feb 2022 08:00:51 +0100
Thread pool ThreadPoolExecutorintroduceThread pool mainly solves two problems: one is that thread pool can provide better performance when executing a large number of asynchronous tasks. When the thread pool is not used, a new thread is required to execute whenever a task needs to be executed. Frequent creation and destruction consume performan ...
Posted by gamblor01 on Thu, 10 Feb 2022 02:06:25 +0100
CountDownLatch is called a subtraction counter. In practical application, according to the explanation of JDK document, there are two scenarios applicable.
Usage 1: as a starting gun, one thread holds the starting gun and orders other threads to start at the same time
Usage 2 is to observe the start or end time of all t ...
Posted by tomd79 on Tue, 08 Feb 2022 04:13:24 +0100
There are two main methods to realize synchronization in Java: Synchronized and Lock. In the previous article, we introduced that Synchronized is an object Lock at the JVM level. Its implementation is based on the MESA management model, while Lock is implemented by Jdk. It is also based on the MESA management model. The core implementation of L ...
Posted by brownca on Thu, 27 Jan 2022 09:08:39 +0100