BAEL-2464 - lambdas and final local variables (#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
This commit is contained in:
Marcos Lopez Gonzalez 2019-04-13 07:10:46 +02:00 committed by Josh Cummings
parent 28ae6b644e
commit 9df90567d4
3 changed files with 108 additions and 0 deletions

19
core-java-lambdas/pom.xml Normal file
View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-lambdas</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
</parent>
</project>

View File

@ -0,0 +1,88 @@
package com.baeldung.lambdas;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Supplier;
import java.util.stream.IntStream;
/**
* Class with examples about working with capturing lambdas.
*/
public class LambdaVariables {
private volatile boolean run = true;
private int start = 0;
private ExecutorService executor = Executors.newFixedThreadPool(3);
public static void main(String[] args) {
new LambdaVariables().localVariableMultithreading();
}
Supplier<Integer> incrementer(int start) {
return () -> start; // can't modify start parameter inside the lambda
}
Supplier<Integer> incrementer() {
return () -> start++;
}
public void localVariableMultithreading() {
boolean run = true;
executor.execute(() -> {
while (run) {
// do operation
}
});
// commented because it doesn't compile, it's just an example of non-final local variables in lambdas
// run = false;
}
public void instanceVariableMultithreading() {
executor.execute(() -> {
while (run) {
// do operation
}
});
run = false;
}
/**
* WARNING: always avoid this workaround!!
*/
public void workaroundSingleThread() {
int[] holder = new int[] { 2 };
IntStream sums = IntStream
.of(1, 2, 3)
.map(val -> val + holder[0]);
holder[0] = 0;
System.out.println(sums.sum());
}
/**
* WARNING: always avoid this workaround!!
*/
public void workaroundMultithreading() {
int[] holder = new int[] { 2 };
Runnable runnable = () -> System.out.println(IntStream
.of(1, 2, 3)
.map(val -> val + holder[0])
.sum());
new Thread(runnable).start();
// simulating some processing
try {
Thread.sleep(new Random().nextInt(3) * 1000L);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
holder[0] = 0;
}
}

View File

@ -381,6 +381,7 @@
<!-- <module>core-java-12</module> --> <!-- We haven't upgraded to java 12. Fixing in BAEL-10841 -->
<module>core-java-8</module>
<module>core-java-8-2</module>
<module>core-java-lambdas</module>
<!--<module>core-java-9</module> --> <!-- We haven't upgraded to java 9. Fixing in BAEL-10841 -->
<!--<module>core-java-os</module> --> <!-- We haven't upgraded to java 9.-->
<module>core-java-arrays</module>