Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

build status codecov Maven Central

XSync Library

What is it

XSync is a thread-safe mutex factory, that provide ability to synchronize by the value of the object(not by the object).

And you can use it for all type of objects which you need.

XSync mutex behavior

You can read more about this library here: Synchronized by the value of the object

Add dependencies

You need to add the next dependencies:


Create the XSync instance

You can create XSync instances parametrized by the type of key which you need. For example we create two XSync instances for Integer and String keys and made it as Spring beans:

public class XSyncConfig {
    public XSync<Integer> intXSync(){
        return new XSync<>();
    public XSync<String> xSync(){
        return new XSync<>();

Use it

Simple example

private XSync<String> xSync;

public void testLock() throws InterruptedException {
    // Arrange
    NonAtomicInt variable = new NonAtomicInt(0);
    ExecutorService executorService = Executors.newFixedThreadPool(10);

    // Act
    executorService.submit(() -> {
        System.out.println("firstThread started.");
        xSync.execute(new String("key"), () -> {
            System.out.println("firstThread took a lock");
            System.out.println("firstThread released a look");

    executorService.submit(() -> {
        System.out.println("secondThread started.");
        xSync.execute(new String("key"), () -> {
            System.out.println("secondThread took a lock");

            // Assert
            System.out.println("secondThread released a look");

    executorService.awaitTermination(5, TimeUnit.SECONDS);

    // Assert

Result of this test:


Example of usage in a banking system

You can read more details about this example in my article: Synchronized by the value of the object

A business logic, that we need to synchronize:

public class PaymentService {


    private XSync<UUID> xSync;

    public void withdrawMoney(UUID userId, int amountOfMoney) {
        xSync.execute(userId, () -> {  
            Result result = externalCashBackService.evaluateCashBack(userId, amountOfMoney); 
            accountService.transfer(userId, amountOfMoney + result.getCashBackAmount()); 
            externalCashBackService.cashBackComplete(userId, result.getCashBackAmount()); 

And places of usages:

public void threadA() {
    paymentService.withdrawMoney(UUID.fromString("11111111-2222-3333-4444-555555555555"), 1000);

public void threadB() {
    paymentService.withdrawMoney(UUID.fromString("11111111-2222-3333-4444-555555555555"), 5000);

Examples on github

You can find a project with examples here:


XSync is Open Source Software released under the Apache 2.0 license.