Java concurrent synchronized keyword and Lock interface

Welcome to read and communicate with us. Please leave a message if you have any questions. There are also open sources on GitHub JavaHouse , welcome ...

Welcome to read and communicate with us. Please leave a message if you have any questions.
There are also open sources on GitHub JavaHouse , welcome star

Quote

As we develop, we encounter concurrency problems.How to solve it?

One solution, simple and rude: lock.All armies and horses were blocked, and only one person was allowed to cross the canopy bridge.Written means to turn a parallel program into a serial program.Realistic locks include door locks, padlocks, drawer locks, and so on.In Java, our locks are the synchronized keyword and the Lock interface.

synchronized keyword

Synchronized, also known as synchronized lock, is a keyword in Java.We can guess that the synchronized principle is also associated with JVM virtual machines.

Synchronized locks are objects.Inside the object is something called a monitor, which monitors the mutex locks of the operating system.Operating system switch threads are essentially programmed from the user-state core state (two states of the cpu).This is a bit expensive, so synchronized, a heavy lock, has also been introduced behind it with a bias lock and a lightweight lock.

Locking (monitoring lock monitor) process analysis ():

  1. When the number of monitor s entering is 0, thread A enters
  2. The number of monitor s entering is 1
  3. Thread B will be blocked if it wants to enter the monitor.

Thread A can enter the monitor repeatedly, so synchronized is a reentrant lock, just like Lock implements.

  • Program Verification
public class SynchronizedTest { private static int i = 0; public static void main(String[] args) { test(); } public static void test(){ synchronized (SynchronizedTest.class){ synchronized (SynchronizedTest.class){ i++; } } } }
  • Run Results

Program is running normally with no errors

synchronized can decorate methods as well as code blocks, which are examples of re-locking above.

  • Modification Method
public class SynchronizedTest { static int n = 100; final static CountDownLatch start = new CountDownLatch(n); private static int i = 0; public static void main(String[] args) throws InterruptedException { for (int j = 0; j < n; j++) { Thread thread = new Thread(new addNoSynchronized()); thread.start(); } start.await(); System.out.println(i); } public static class addSynchronized implements Runnable{ @Override public void run() { addSynchronized(); } public static synchronized void addSynchronized(){ for (int j = 0; j < 1000; j++) { i++; } start.countDown(); } } }
  • Run Results
100000

If the synchronized keyword is omitted, the run result is probably not 100,000 because of thread insecurity.

Lock interface

Typically, we use the ReentrantLock class as a reentrant lock to implement the Lock interface.

  • Usage method
public class ReentranLockTest { private static int j; private static int n = 100; private static CountDownLatch latch = new CountDownLatch(n); public static void main(String[] args) throws InterruptedException { for (int i = 0; i < n; i++) { new Thread(new LockTest()).start(); } latch.await(); System.out.println("The results are:"+j); } public static class LockTest implements Runnable{ static Lock lock = new ReentrantLock(); @Override public void run() { lockTest(); latch.countDown(); } private void lockTest() { lock.lock(); try { for (int i = 0; i < 1000; i++) { j++; } }finally { lock.unlock(); } } } }
  • Run Results
The result is: 100000

Here we lock the j++ resource area (public resource). Lock is modified by the static keyword and is a class object. Think about what would happen if it weren't a class object?That's the chain lock (see picture).

Each thread unlocks the lock with a key, which makes no sense to the program.Because we need a lock.

Welcome to my WeChat Public Number

3 December 2019, 03:25 | Views: 9624

Add new comment

For adding a comment, please log in
or create account

0 comments