File tree 3 files changed +48
-0
lines changed
main/java/com/baeldung/lombok/locked
test/java/com/baeldung/lombok/locked
3 files changed +48
-0
lines changed Original file line number Diff line number Diff line change 34
34
</plugins >
35
35
</build >
36
36
37
+ <properties >
38
+ <maven .compiler.source>21</maven .compiler.source>
39
+ <maven .compiler.target>21</maven .compiler.target>
40
+ </properties >
41
+
37
42
</project >
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments