Skip to content

antkorwin/xsync-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

XSync Library examples

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

You can read about XSync in more details here:

Add a dependency

<dependency>
    <groupId>com.antkorwin</groupId>
    <artifactId>xsync</artifactId>
    <version>1.1</version>
</dependency>

Create synchronization primitives

@Configuration
public class XSyncConfig {

    @Bean
    public XSync<UUID> idXSync(){
        return new XSync<>();
    }

    @Bean
    public XSync<Integer> intXSync(){
        return new XSync<>();
    }
}

The sample of usages

@Test
public void testSync() throws InterruptedException {
    String idStr = UUID.randomUUID().toString();
    NonAtomicInt nonAtomicInt = new NonAtomicInt(0);

    IntStream.range(0, ITERATION_CNT)
             .boxed()
             .parallel()
             .forEach(i -> xSync.execute(UUID.fromString(idStr), nonAtomicInt::increment));

    Thread.sleep(1000L);

    Assertions.assertThat(nonAtomicInt.getValue())
              .isEqualTo(ITERATION_CNT);
}

@AllArgsConstructor
@Getter
public class NonAtomicInt {

    private int value;

    public void increment() {
        this.value++;
    }
}

You can see a full example in the source code of this repository.

About

Examples of using the XSync library.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages