Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of synchronizing Code Block between Lock Lock and synchronized in JUC

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to solve the problem of Lock lock and synchronized synchronization code block in JUC". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "how to solve the problem of Lock lock and synchronized synchronization code block in JUC" can help you solve the problem.

1. Lock lock

ReentrantLock class: reusable locks (fair locks | unfair locks)

ReentrantReadWriteLock.ReadLock: read lock

ReentrantReadWriteLock.WriteLock: write lock

Second, the bottom layer of the lock

There are fair locks and unfair locks at the bottom of the lock. Where:

Fair lock: very fair, do not jump the queue.

Unfair lock: very unfair, you can jump the queue. (default unfair lock)

Case 1: traditional synchronized implementation / * synchronized synchronization code block ensures ticket sales thread safety * * @ Author JUNSHI * @ Create 2022-04-10 22:46 * / public class SaleTicketDemo01 {public static void main (String [] args) {Ticket ticket = new Ticket (); new Thread ()-> {for (int I = 0; I)

< 60; i++) { ticket.sale(); } },"AA").start(); },"BB").start(); new Thread(() ->

{}, "CC") .start ();} static class Ticket {/ / 50 floating tickets private int num = 50 / / ticketing synchronized (synchronization code block) essence: queue, lock public synchronized void sale () {if (num > 0) {System.out.println (Thread.currentThread (). GetName () + "sold" + (num--) + "ticket, remaining:" + num) Case 2: implementation of Lock lock / * Lock lock guarantees ticket sales thread safety * * @ Author JUNSHI * @ Create 2022-04-10 22:46 * / public class SaleTicketDemo02 {public static void main (String [] args) {Ticket2 ticket = new Ticket2 (); new Thread ()-> {for (int I = 0; I)

< 60; i++) ticket.sale(); },"AA").start(); new Thread(()->

{for (int I = 0; I)

< 60; i++) ticket.sale(); },"BB").start(); new Thread(()->

{for (int I = 0; I)

< 60; i++) ticket.sale(); },"CC").start(); } static class Ticket2{ // 50张飘票 private int num = 50; // 加锁三部曲 // 1、 创建锁 =>

New ReentrantLock (); / / 2, lock = > lock.lock (); / / 3, release lock = > lock.unlock (); public void sale () {/ / reentrant lock default: unfair lock: very unfair, can jump the queue. (default unfair lock) Lock lock = new ReentrantLock (); / / lock lock.lock (); try {/ / execute business if (num > 0) {System.out.println (Thread.currentThread (). GetName () + "sold" + (num--) + "ticket", remaining: "+ num) }} catch (Exception e) {e.printStackTrace ();} finally {/ / unlock lock.unlock ();} 4. The difference between lock lock and synchronized

Snchronized is the built-in Java keyword; Lock is a Java class.

Synchronized cannot determine the state of the acquired lock; Lock can determine whether the lock has been acquired. Boolean b = lock.tryLock ();)

Synchronized automatically releases the lock; Lock must release the lock manually, or deadlock if the lock is not released.

When synchronized thread 1 acquires lock blocking, thread 2 waits; when Lock lock thread 1 acquires lock blocking, thread 2 waits long enough before interrupting the wait to do something else.

Synchronized reentrant lock: uninterruptible, unfair; Lock reentrant lock: lock can be judged, unfair (can be set by yourself).

Lock.lockInterruptibly (); method: when two threads simultaneously want to acquire a lock through this method, if thread An acquires the lock and thread B is only waiting, then calling the threadB.interrupt () method on thread B can interrupt the waiting process of thread B.

Synchronized is suitable for locking a small amount of code synchronization problems; Lock is suitable for locking a large amount of synchronous code.

This is the end of the content on "how to solve the problem of synchronizing code blocks between Lock locks and synchronized in JUC". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report