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

What is the design structure and details of the Java rewrite lock

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

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "what is the design structure and details of Java rewriting lock". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope this article "what is the design structure and details of Java rewriting lock" can help you solve the problem.

Introduction language

Some interviewers like to ask students to rewrite a new lock after talking about the principle of the lock, requiring you to write a general idea and code logic on the whiteboard. This kind of interview question is quite difficult. Personally, I think it mainly focuses on two parts:

Take a look at how your understanding of the lock principle comes from. If you haven't interpreted the source code, you can also tell the general principle by looking at the articles on the Internet or the test questions on the back. But it is very difficult for you to write a lock implementation code on the spot, unless you have actually read the source code or have experience in lock-related projects.

We don't need to create, we just need to rewrite it by mimicking the existing API in the Java lock.

If you have read the source code, this problem is really simple, you can choose a lock you are familiar with to imitate.

1. Demand

In general, when customizing locks, we define them according to requirements, and it is impossible to define locks out of thin air. When it comes to shared locks, you may think of many scenarios. For example, read locks for shared resources can be shared, such as shared access to database links, for example, the number of links on the Socket server can be shared. There are many scenarios. We chose the shared access database link scenario to define a lock.

2. Detailed design

Suppose (the following assumptions are all hypothetical) our database is a stand-alone mysql, can only bear 10 links, when creating database links, we are through the most original JDBC way, we use an interface to encapsulate the process of creating links with JDBC, this interface we named: create link interface.

The overall requirements for shared access database links are as follows: the maximum number of mysql links added up to all requests is no more than 10 (including 10). Once it exceeds 10, an error is reported directly.

Against this background, we have designed the following figure:

The most critical part of this design is that we decide whether we can get the mysql link through whether we can get the lock. If we can get the lock, then we can get the link, otherwise we will report an error directly.

Then let's take a look at the landing code:

2.1. Define the lock

First, we need to define a lock, which needs to have two elements:

Definition of lock: synchronizer Sync; lock provides external locking and unlocking methods.

The code implementation of the shared lock is as follows:

/ / sharing unfair lock public class ShareLock implements Serializable {/ / Synchronizer private final Sync sync; / / is used to ensure that the maximum value private final int maxCount; / * is assigned to the synchronizer sync during initialization * count represents the maximum value that can be obtained from the shared lock * / public ShareLock (int count) {this.sync = new Sync (count); maxCount = count } / * * acquire lock * @ return true: successfully acquired lock; false: failed * / public boolean lock () {return sync.acquireByShared (1);} / * release lock * @ return true: successfully released lock; false: failed * / public boolean unLock () {return sync.releaseShared (1);}}

As can be seen from the above code, the implementation of locking and releasing locks depends on the underlying implementation of the synchronizer Sync.

The only thing to note is that the lock needs to specify the specification of API, mainly in two aspects:

What is needed for API, that is, what parameters do you need to pass to me during lock initialization? when ShareLock initialization, you need to pass the maximum number of shareable locks

You need to define your own capabilities, that is, the input and output parameters of each method. In the implementation of ShareLock, there are no input parameters for locking or releasing locks, which is a dead 1 in the method, indicating that the lock can only be locked or released once each time the method is executed. The output parameter is a Boolean value, true indicates that locking or releasing the lock succeeded, false indicates failure, and the underlying layer uses Sync unfair locks.

The above way of thinking is methodological, that is, when we think about a problem, we can start from two aspects: what is API? What is the ability of API?

2.2.Defining Synchronizer Sync

Sync inherits AQS directly. The code is as follows:

Class Sync extends AbstractQueuedSynchronizer {/ / indicates that there are at most count shared locks that can obtain public Sync (int count) {setState (count);} / obtain I locks public boolean acquireByShared (int I) {/ / spin guarantee CAS must be successful for (;;) {if (I)

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