Skip to content

Commit 26d20c7

Browse files
authored
BAEL-7778 - A Guide to @locked in Lombok (#17765)
* BAEL-7778 - A Guide to @locked in Lombok * Update java version
1 parent d8b5538 commit 26d20c7

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

lombok-modules/lombok-3/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,9 @@
3434
</plugins>
3535
</build>
3636

37+
<properties>
38+
<maven.compiler.source>21</maven.compiler.source>
39+
<maven.compiler.target>21</maven.compiler.target>
40+
</properties>
41+
3742
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.lombok.locked;
2+
3+
import lombok.Locked;
4+
5+
public class Counter {
6+
7+
private int counter = 0;
8+
9+
@Locked.Write
10+
public void increment() {
11+
counter++;
12+
}
13+
14+
@Locked.Read
15+
public int get() {
16+
return counter;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.lombok.locked;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class CounterUnitTest {
8+
9+
@Test
10+
void givenCounter_whenIncrementCalledMultipleTimes_thenReturnCorrectResult() throws InterruptedException {
11+
Counter counter = new Counter();
12+
13+
Thread.Builder builder = Thread.ofVirtual()
14+
.name("worker-", 0);
15+
Runnable task = counter::increment;
16+
17+
Thread t1 = builder.start(task);
18+
t1.join();
19+
20+
Thread t2 = builder.start(task);
21+
t2.join();
22+
23+
assertEquals(2, counter.get());
24+
}
25+
}

0 commit comments

Comments
 (0)