Skip to content

Concurrency helpers #156

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

Merged
merged 4 commits into from
Nov 19, 2024
Merged

Concurrency helpers #156

merged 4 commits into from
Nov 19, 2024

Conversation

fh-ms
Copy link
Contributor

@fh-ms fh-ms commented Nov 19, 2024

New helpers for concurrency handling using ReentrantReadWriteLocks internally.

LockedExecutor and StripeLockedExecutor are utilities that use ReentrantReadWriteLock internally.

LockScope and StripeLockScope are abstract classes to extend. They provide protected delegates for the lock executors but hide the executor itself for even easier usage.

Use executors

public class Customers
{
	private final transient LockedExecutor executor = LockedExecutor.New();
	private final List<Customer> customers = new ArrayList<>();
	
	public void addCustomer(Customer c)
	{
		this.executor.write(() -> {
			this.customers.add(c);
			Application.storageManager().store(this.customers);
		});
	}
	
	public void traverseCustomers(Consumer<Customer> consumer)
	{
		this.executor.read(() -> this.customers.forEach(consumer));
	}
}

Or extend from a scope

public class Customers extends LockScope
{
	private final List<Customer> customers = new ArrayList<>();
	
	public void addCustomer(Customer c)
	{
		write(() -> {
			this.customers.add(c);
			Application.storageManager().store(this.customers);
		});
	}
	
	public void traverseCustomers(Consumer<Customer> consumer)
	{
		read(() -> this.customers.forEach(consumer));
	}
}

@fh-ms fh-ms added the enhancement New feature or request label Nov 19, 2024
@fh-ms fh-ms added this to the 2.1.0 milestone Nov 19, 2024
@fh-ms fh-ms requested review from zdenek-jonas and hg-ms November 19, 2024 11:24
@fh-ms fh-ms self-assigned this Nov 19, 2024
@Bios-Marcel
Copy link
Contributor

Bios-Marcel commented Nov 19, 2024

This is definitely useful, we did something like that too.

However, shouldn't this be part of the store, instead of the serializer? Just wondering, maybe the question is dumb cause I missunderstood something :D

@fh-ms
Copy link
Contributor Author

fh-ms commented Nov 19, 2024

This is definitely useful, we did something like that too.

However, shouldn't this be part of the store, instead of the serializer? Just wondering, maybe the question is dumb cause I missunderstood something :D

It's part of the serializer's base module like all other common utilities, which could be useful for any other module.

@fh-ms fh-ms merged commit a17e469 into main Nov 19, 2024
8 checks passed
@fh-ms fh-ms deleted the concurrency-helper branch November 19, 2024 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants