Skip to content

Commit fa74f05

Browse files
authored
Merge pull request eugenp#3921 from denis-zhdanov/BAEL-1423-jctools
BAEL-1423 Java Concurrency Utility with JCTools Library
2 parents ed9f3fa + 0bb4359 commit fa74f05

4 files changed

Lines changed: 219 additions & 0 deletions

File tree

‎libraries/pom.xml‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,12 @@
686686
<artifactId>fugue</artifactId>
687687
<version>4.5.1</version>
688688
</dependency>
689+
690+
<dependency>
691+
<groupId>org.jctools</groupId>
692+
<artifactId>jctools-core</artifactId>
693+
<version>${jctools.version}</version>
694+
</dependency>
689695
</dependencies>
690696

691697
<repositories>
@@ -785,6 +791,52 @@
785791
</archive>
786792
</configuration>
787793
</plugin>
794+
<!-- /Neuroph -->
795+
<plugin>
796+
<artifactId>maven-compiler-plugin</artifactId>
797+
<version>3.7.0</version>
798+
<configuration>
799+
<source>1.8</source>
800+
<target>1.8</target>
801+
</configuration>
802+
</plugin>
803+
804+
<!-- Borrowed from the 'jmh-java-benchmark-archetype' pom.xml -->
805+
<plugin>
806+
<groupId>org.apache.maven.plugins</groupId>
807+
<artifactId>maven-shade-plugin</artifactId>
808+
<version>2.2</version>
809+
<executions>
810+
<execution>
811+
<phase>package</phase>
812+
<goals>
813+
<goal>shade</goal>
814+
</goals>
815+
<configuration>
816+
<finalName>benchmarks</finalName>
817+
<transformers>
818+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
819+
<mainClass>org.openjdk.jmh.Main</mainClass>
820+
</transformer>
821+
</transformers>
822+
<filters>
823+
<filter>
824+
<!--
825+
Shading signed JARs will fail without this.
826+
http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar
827+
-->
828+
<artifact>*:*</artifact>
829+
<excludes>
830+
<exclude>META-INF/*.SF</exclude>
831+
<exclude>META-INF/*.DSA</exclude>
832+
<exclude>META-INF/*.RSA</exclude>
833+
</excludes>
834+
</filter>
835+
</filters>
836+
</configuration>
837+
</execution>
838+
</executions>
839+
</plugin>
788840
</plugins>
789841
</build>
790842

@@ -856,6 +908,7 @@
856908
<infinispan.version>9.1.5.Final</infinispan.version>
857909
<opencsv.version>4.1</opencsv.version>
858910
<unirest.version>1.4.9</unirest.version>
911+
<jctools.version>2.1.2</jctools.version>
859912
<commons-codec-version>1.10.L001</commons-codec-version>
860913
<jets3t-version>0.9.4.0006L</jets3t-version>
861914
</properties>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.baeldung.jctools;
2+
3+
import org.jctools.queues.MpmcArrayQueue;
4+
import org.jctools.queues.atomic.MpmcAtomicArrayQueue;
5+
import org.openjdk.jmh.annotations.*;
6+
import org.openjdk.jmh.infra.Control;
7+
8+
import java.util.Queue;
9+
import java.util.concurrent.ArrayBlockingQueue;
10+
import java.util.concurrent.TimeUnit;
11+
12+
@BenchmarkMode(Mode.SampleTime)
13+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
14+
@Fork(1)
15+
@Warmup(iterations = 1)
16+
@Measurement(iterations = 3)
17+
@State(Scope.Group)
18+
public class MpmcBenchmark {
19+
20+
public static final String PARAM_UNSAFE = "MpmcArrayQueue";
21+
public static final String PARAM_AFU = "MpmcAtomicArrayQueue";
22+
public static final String PARAM_JDK = "ArrayBlockingQueue";
23+
24+
public static final int PRODUCER_THREADS_NUMBER = 32;
25+
public static final int CONSUMER_THREADS_NUMBER = 32;
26+
27+
public static final String GROUP_NAME = "MyGroup";
28+
29+
public static final int CAPACITY = 128;
30+
31+
@Param({PARAM_UNSAFE, PARAM_AFU, PARAM_JDK})
32+
public volatile String implementation;
33+
34+
public volatile Queue<Long> queue;
35+
36+
@Setup(Level.Trial)
37+
public void setUp() {
38+
switch (implementation) {
39+
case PARAM_UNSAFE:
40+
queue = new MpmcArrayQueue<>(CAPACITY);
41+
break;
42+
case PARAM_AFU:
43+
queue = new MpmcAtomicArrayQueue<>(CAPACITY);
44+
break;
45+
case PARAM_JDK:
46+
queue = new ArrayBlockingQueue<>(CAPACITY);
47+
break;
48+
default:
49+
throw new UnsupportedOperationException("Unsupported implementation " + implementation);
50+
}
51+
}
52+
53+
54+
@Benchmark
55+
@Group(GROUP_NAME)
56+
@GroupThreads(PRODUCER_THREADS_NUMBER)
57+
public void write(Control control) {
58+
//noinspection StatementWithEmptyBody
59+
while (!control.stopMeasurement && !queue.offer(1L)) {
60+
// Is intentionally left blank
61+
}
62+
}
63+
64+
@Benchmark
65+
@Group(GROUP_NAME)
66+
@GroupThreads(CONSUMER_THREADS_NUMBER)
67+
public void read(Control control) {
68+
//noinspection StatementWithEmptyBody
69+
while (!control.stopMeasurement && queue.poll() == null) {
70+
// Is intentionally left blank
71+
}
72+
}
73+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## How to build and run the JMH benchmark
2+
3+
Execute the following from the project's root:
4+
```bash
5+
mvn clean install
6+
java -jar ./target/benchmarks.jar MpmcBenchmark -si true
7+
```
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.baeldung.jctools;
2+
3+
import org.jctools.queues.SpscArrayQueue;
4+
import org.jctools.queues.SpscChunkedArrayQueue;
5+
import org.junit.Test;
6+
7+
import java.util.HashSet;
8+
import java.util.Set;
9+
import java.util.concurrent.CountDownLatch;
10+
import java.util.concurrent.atomic.AtomicReference;
11+
import java.util.function.IntConsumer;
12+
import java.util.stream.Collectors;
13+
import java.util.stream.IntStream;
14+
15+
import static org.assertj.core.api.Assertions.assertThat;
16+
import static org.assertj.core.api.Assertions.fail;
17+
18+
public class JCToolsUnitTest {
19+
20+
@Test
21+
public void givenMultipleProducers_whenSpscQueueUsed_thenNoWarningOccurs() throws InterruptedException {
22+
SpscArrayQueue<Integer> queue = new SpscArrayQueue<Integer>(2);
23+
24+
Thread producer1 = new Thread(() -> {
25+
queue.offer(1);
26+
});
27+
producer1.start();
28+
producer1.join();
29+
30+
Thread producer2 = new Thread(() -> {
31+
queue.offer(2);
32+
});
33+
producer2.start();
34+
producer2.join();
35+
36+
Set<Integer> fromQueue = new HashSet<>();
37+
Thread consumer = new Thread(() -> queue.drain(fromQueue::add));
38+
consumer.start();
39+
consumer.join();
40+
41+
assertThat(fromQueue).containsOnly(1, 2);
42+
}
43+
44+
@Test
45+
public void whenQueueIsFull_thenNoMoreElementsCanBeAdded() throws InterruptedException {
46+
SpscChunkedArrayQueue<Integer> queue = new SpscChunkedArrayQueue<>(8, 16);
47+
assertThat(queue.capacity()).isEqualTo(16);
48+
49+
CountDownLatch startConsuming = new CountDownLatch(1);
50+
CountDownLatch awakeProducer = new CountDownLatch(1);
51+
AtomicReference<Throwable> error = new AtomicReference<>();
52+
Thread producer = new Thread(() -> {
53+
IntStream.range(0, queue.capacity()).forEach(i -> {
54+
assertThat(queue.offer(i)).isTrue();
55+
});
56+
assertThat(queue.offer(queue.capacity())).isFalse();
57+
startConsuming.countDown();
58+
try {
59+
awakeProducer.await();
60+
} catch (InterruptedException e) {
61+
throw new RuntimeException(e);
62+
}
63+
64+
assertThat(queue.offer(queue.capacity())).isTrue();
65+
});
66+
producer.setUncaughtExceptionHandler((t, e) -> {
67+
error.set(e);
68+
startConsuming.countDown();
69+
});
70+
producer.start();
71+
72+
startConsuming.await();
73+
74+
if (error.get() != null) {
75+
fail("Producer's assertion failed", error.get());
76+
}
77+
78+
Set<Integer> fromQueue = new HashSet<>();
79+
queue.drain(fromQueue::add);
80+
awakeProducer.countDown();
81+
producer.join();
82+
queue.drain(fromQueue::add);
83+
84+
assertThat(fromQueue).containsAll(IntStream.range(0, 17).boxed().collect(Collectors.toSet()));
85+
}
86+
}

0 commit comments

Comments
 (0)