{"id":14977533,"url":"https://github.com/antkorwin/xsync","last_synced_at":"2025-10-28T04:31:30.159Z","repository":{"id":41714896,"uuid":"137748898","full_name":"antkorwin/xsync","owner":"antkorwin","description":"Tools for the synchronization on the instance of key.","archived":false,"fork":false,"pushed_at":"2023-08-03T06:21:52.000Z","size":162,"stargazers_count":81,"open_issues_count":11,"forks_count":23,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-02-01T11:24:49.555Z","etag":null,"topics":["concurrency","mutex","spring-framework","synchronization","synchronized"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/antkorwin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-06-18T12:27:01.000Z","updated_at":"2024-11-08T06:54:43.000Z","dependencies_parsed_at":"2024-09-18T22:05:25.046Z","dependency_job_id":null,"html_url":"https://github.com/antkorwin/xsync","commit_stats":{"total_commits":78,"total_committers":5,"mean_commits":15.6,"dds":"0.20512820512820518","last_synced_commit":"6e46fbfd1fa0bf36ffd214f51057009f1051476a"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antkorwin%2Fxsync","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antkorwin%2Fxsync/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antkorwin%2Fxsync/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antkorwin%2Fxsync/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/antkorwin","download_url":"https://codeload.github.com/antkorwin/xsync/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238597386,"owners_count":19498396,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["concurrency","mutex","spring-framework","synchronization","synchronized"],"created_at":"2024-09-24T13:55:50.163Z","updated_at":"2025-10-28T04:31:29.136Z","avatar_url":"https://github.com/antkorwin.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"![build status](https://github.com/antkorwin/xsync/actions/workflows/build.yml/badge.svg)\n[![codecov](https://codecov.io/gh/antkorwin/xsync/branch/master/graph/badge.svg)](https://codecov.io/gh/antkorwin/xsync)\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.antkorwin/xsync/badge.svg)](https://search.maven.org/search?q=g:com.antkorwin%20AND%20a:xsync)\n\n# XSync Library\n\n\n## What is it\n\nXSync is a thread-safe mutex factory, that provide \nability to synchronize by the value of the object(not by the object).\n\nAnd you can use it for all type of objects which you need.\n\n![XSync mutex behavior](http://antkorwin.com/concurrency/diag-0672834a7737bb323990aabe3bcb5ce6.png)\n\nYou can read more about this library here: \n[Synchronized by the value of the object](http://antkorwin.com/concurrency/synchronization_by_value.html) \n\n## Add dependencies \n\nYou need to add the next dependencies:\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.antkorwin\u003c/groupId\u003e\n    \u003cartifactId\u003exsync\u003c/artifactId\u003e\n    \u003cversion\u003e1.3\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n## Create the XSync instance \n\nYou can create XSync instances parametrized by the type of key which you need.\nFor example we create two XSync instances for Integer and String keys and made it as Spring beans:\n\n```java\n@Configuration\npublic class XSyncConfig {\n   \n    @Bean\n    public XSync\u003cInteger\u003e intXSync(){\n        return new XSync\u003c\u003e();\n    }\n    \n    @Bean\n    public XSync\u003cString\u003e xSync(){\n        return new XSync\u003c\u003e();\n    }\n}\n```\n\n## Use it\n\n\n### Simple example \n```java\n@Autowired\nprivate XSync\u003cString\u003e xSync;\n\n@Test\npublic void testLock() throws InterruptedException {\n    // Arrange\n    NonAtomicInt variable = new NonAtomicInt(0);\n    ExecutorService executorService = Executors.newFixedThreadPool(10);\n\n    // Act\n    executorService.submit(() -\u003e {\n        System.out.println(\"firstThread started.\");\n        xSync.execute(new String(\"key\"), () -\u003e {\n            System.out.println(\"firstThread took a lock\");\n            sleep(2);\n            variable.increment();\n            System.out.println(\"firstThread released a look\");\n        });\n    });\n\n    executorService.submit(() -\u003e {\n        sleep(1);\n        System.out.println(\"secondThread started.\");\n        xSync.execute(new String(\"key\"), () -\u003e {\n            System.out.println(\"secondThread took a lock\");\n\n            // Assert\n            Assertions.assertThat(variable.getValue()).isEqualTo(1);\n            sleep(1);\n            variable.increment();\n            System.out.println(\"secondThread released a look\");\n        });\n    });\n\n    executorService.awaitTermination(5, TimeUnit.SECONDS);\n\n    // Assert\n    Assertions.assertThat(variable.getValue()).isEqualTo(2);\n} \n\n``` \n\nResult of this test:\n\n![result](http://antkorwin.com/concurrency/lock_test.png)\n\n\n### Example of usage in a banking system \n\nYou can read more details about this example in my article: \n[Synchronized by the value of the object](http://antkorwin.com/concurrency/synchronization_by_value.html)\n\nA business logic, that we need to synchronize:\n\n```java\npublic class PaymentService {\n\n    ...\n\n    @Autowired\n    private XSync\u003cUUID\u003e xSync;\n\n    public void withdrawMoney(UUID userId, int amountOfMoney) {\n        xSync.execute(userId, () -\u003e {  \n            Result result = externalCashBackService.evaluateCashBack(userId, amountOfMoney); \n            accountService.transfer(userId, amountOfMoney + result.getCashBackAmount()); \n            externalCashBackService.cashBackComplete(userId, result.getCashBackAmount()); \n        });\n    }\n}\n```\n\nAnd places of usages:\n\n```java\npublic void threadA() {\n    paymentService.withdrawMoney(UUID.fromString(\"11111111-2222-3333-4444-555555555555\"), 1000);\n}\n\n\npublic void threadB() {\n    paymentService.withdrawMoney(UUID.fromString(\"11111111-2222-3333-4444-555555555555\"), 5000);\n}\n```\n\n\n### Examples on github\n\nYou can find a project with examples here: [github.com/antkorwin/xsync-example](https://github.com/antkorwin/xsync-example)\n\n\n## License\n\nXSync is Open Source Software released under the [Apache 2.0 license](https://www.apache.org/licenses/LICENSE-2.0.html).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantkorwin%2Fxsync","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fantkorwin%2Fxsync","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantkorwin%2Fxsync/lists"}