Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

unminnn
Copy link

@unminnn unminnn commented Dec 7, 2024

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.

Copy link

sonarqubecloud bot commented Dec 7, 2024

@unminnn
Copy link
Author

unminnn commented Dec 8, 2024

More comprehensive test cases are being worked on currently.

Comment on lines +1 to +12
---
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
---
Copy link
Owner

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
---

Comment on lines +14 to +18
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Copy link
Owner

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

Comment on lines +20 to +25
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.10.2</version>
<scope>test</scope>
</dependency>
Copy link
Owner

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?

Comment on lines +68 to +85
/**
* 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;
}
}
Copy link
Owner

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

Comment on lines +11 to +15
/**
* Program entry point.
*
* @param args command line args
*/
Copy link
Owner

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

Comment on lines +18 to +27
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");
});
Copy link
Owner

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.

@iluwatar
Copy link
Owner

iluwatar commented Jan 6, 2025

Additionally, this module should be added to parent pom.xml. Otherwise, CI does not even build it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Coarse-Grained Lock pattern
2 participants