-
-
Notifications
You must be signed in to change notification settings - Fork 26.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented Coarse-Grained pattern issue #1289 #3124
base: master
Are you sure you want to change the base?
Conversation
Quality Gate passedIssues Measures |
More comprehensive test cases are being worked on currently. |
--- | ||
Title: "Coarse-Grained Lock Pattern in Java: Simplifying Thread-Safe Operations" | ||
Short Title: Coarse-Grained Lock | ||
Description: "Coarse-Grained Lock pattern ensures thread safety by holding a global lock for related objects instead of multiple locks." | ||
Category: Concurrency | ||
Tags: | ||
|
||
Concurrency | ||
Synchronization | ||
Thread Safety | ||
Locking Mechanisms | ||
--- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please follow the frontmatter format exactly. Here is an example from another pattern:
---
title: "Abstract Document Pattern in Java: Simplifying Data Handling with Flexibility"
shortTitle: Abstract Document
description: "Explore the Abstract Document design pattern in Java. Learn its intent, explanation, applicability, benefits, and see real-world examples to implement flexible and dynamic data structures."
category: Structural
language: en
tag:
- Abstraction
- Decoupling
- Dynamic typing
- Encapsulation
- Extensibility
- Polymorphism
---
<properties> | ||
<maven.compiler.source>17</maven.compiler.source> | ||
<maven.compiler.target>17</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed, comes from parent pom.xml
<dependency> | ||
<groupId>org.testng</groupId> | ||
<artifactId>testng</artifactId> | ||
<version>7.10.2</version> | ||
<scope>test</scope> | ||
</dependency> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why we need this? Could we stick to JUnit instead?
/** | ||
* Returns the city where the address is located. | ||
* | ||
* @return the city name | ||
*/ | ||
public String getCity() { | ||
return city; | ||
} | ||
|
||
/** | ||
* Updates the city name with the specified value. | ||
* | ||
* @param city the new city name | ||
*/ | ||
public void setCity(String city) { | ||
this.city = city; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use Lombok to get rid of boilerplate like getters and setters
/** | ||
* Program entry point. | ||
* | ||
* @param args command line args | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please explain the pattern briefly and describe how this example implements it
Lock lock = new Lock(); | ||
Customer customer = new Customer(55, "john"); | ||
Address address1 = new Address(customer.getCustomerId(), 1, "chicago"); | ||
Address address2 = new Address(customer.getCustomerId(), 2, "houston"); | ||
|
||
lock.synchronizedMethod(() -> { | ||
customer.setName("smith"); | ||
address1.setCity("dallas"); | ||
address2.setCity("phoenix"); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add comments to the code. Remember this is learning material.
Additionally, this module should be added to parent pom.xml. Otherwise, CI does not even build it. |
What problem does this PR solve?
Close #1289
This PR implements the Coarse-Grained Lock pattern to lock multiple objects instead of a separate lock for each one, and includes an example of a customer and multiple addresses.