Skip to content

Commit 9df9056

Browse files
marcos-lgjzheaux
authored andcommitted
BAEL-2464 - lambdas and final local variables (eugenp#6530)
* lambdas and final local variables * workarounds when using non-final local variables in lambdas * cleanup * capturing lambdas refactor * lambdas and local variables * added comments to capturing lambdas examples * lambdas variables moved to core-java-lambdas * core-java-lambdas module moved in pom * format
1 parent 28ae6b6 commit 9df9056

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

core-java-lambdas/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>core-java-lambdas</artifactId>
7+
<version>0.1.0-SNAPSHOT</version>
8+
<name>core-java</name>
9+
<packaging>jar</packaging>
10+
11+
<parent>
12+
<groupId>com.baeldung</groupId>
13+
<artifactId>parent-java</artifactId>
14+
<version>0.0.1-SNAPSHOT</version>
15+
<relativePath>../parent-java</relativePath>
16+
</parent>
17+
18+
19+
</project>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.baeldung.lambdas;
2+
3+
import java.util.Random;
4+
import java.util.concurrent.ExecutorService;
5+
import java.util.concurrent.Executors;
6+
import java.util.function.Supplier;
7+
import java.util.stream.IntStream;
8+
9+
/**
10+
* Class with examples about working with capturing lambdas.
11+
*/
12+
public class LambdaVariables {
13+
14+
private volatile boolean run = true;
15+
private int start = 0;
16+
17+
private ExecutorService executor = Executors.newFixedThreadPool(3);
18+
19+
public static void main(String[] args) {
20+
new LambdaVariables().localVariableMultithreading();
21+
}
22+
23+
Supplier<Integer> incrementer(int start) {
24+
return () -> start; // can't modify start parameter inside the lambda
25+
}
26+
27+
Supplier<Integer> incrementer() {
28+
return () -> start++;
29+
}
30+
31+
public void localVariableMultithreading() {
32+
boolean run = true;
33+
executor.execute(() -> {
34+
while (run) {
35+
// do operation
36+
}
37+
});
38+
// commented because it doesn't compile, it's just an example of non-final local variables in lambdas
39+
// run = false;
40+
}
41+
42+
public void instanceVariableMultithreading() {
43+
executor.execute(() -> {
44+
while (run) {
45+
// do operation
46+
}
47+
});
48+
49+
run = false;
50+
}
51+
52+
/**
53+
* WARNING: always avoid this workaround!!
54+
*/
55+
public void workaroundSingleThread() {
56+
int[] holder = new int[] { 2 };
57+
IntStream sums = IntStream
58+
.of(1, 2, 3)
59+
.map(val -> val + holder[0]);
60+
61+
holder[0] = 0;
62+
63+
System.out.println(sums.sum());
64+
}
65+
66+
/**
67+
* WARNING: always avoid this workaround!!
68+
*/
69+
public void workaroundMultithreading() {
70+
int[] holder = new int[] { 2 };
71+
Runnable runnable = () -> System.out.println(IntStream
72+
.of(1, 2, 3)
73+
.map(val -> val + holder[0])
74+
.sum());
75+
76+
new Thread(runnable).start();
77+
78+
// simulating some processing
79+
try {
80+
Thread.sleep(new Random().nextInt(3) * 1000L);
81+
} catch (InterruptedException e) {
82+
throw new RuntimeException(e);
83+
}
84+
85+
holder[0] = 0;
86+
}
87+
88+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@
381381
<!-- <module>core-java-12</module> --> <!-- We haven't upgraded to java 12. Fixing in BAEL-10841 -->
382382
<module>core-java-8</module>
383383
<module>core-java-8-2</module>
384+
<module>core-java-lambdas</module>
384385
<!--<module>core-java-9</module> --> <!-- We haven't upgraded to java 9. Fixing in BAEL-10841 -->
385386
<!--<module>core-java-os</module> --> <!-- We haven't upgraded to java 9.-->
386387
<module>core-java-arrays</module>

0 commit comments

Comments
 (0)