Merge pull request '修改编译错误' (#19) from active_mq into main
Reviewed-on: #19
This commit is contained in:
commit
5c0bd4b481
|
@ -1,88 +0,0 @@
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.java8.lambda.serialization;
|
package com.ossez.java8.lambda.serialization;
|
||||||
|
|
||||||
public class NotSerializableLambdaExpression {
|
public class NotSerializableLambdaExpression {
|
||||||
public static Object getLambdaExpressionObject() {
|
public static Object getLambdaExpressionObject() {
|
|
@ -1,5 +1,6 @@
|
||||||
package com.baeldung.java8.lambda.serialization;
|
package com.baeldung.java8.lambda.serialization;
|
||||||
|
|
||||||
|
import com.ossez.java8.lambda.serialization.NotSerializableLambdaExpression;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
|
@ -65,7 +65,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>mysql</groupId>
|
<groupId>mysql</groupId>
|
||||||
<artifactId>mysql-connector-java</artifactId>
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
<version>${mysql.connector.version}</version>
|
<version>${mysql-connector-j.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
@ -75,7 +75,7 @@
|
||||||
<properties>
|
<properties>
|
||||||
<activejdbc.version>3.4-j11</activejdbc.version>
|
<activejdbc.version>3.4-j11</activejdbc.version>
|
||||||
<environments>development.test,development</environments>
|
<environments>development.test,development</environments>
|
||||||
<mysql.connector.version>8.0.24</mysql.connector.version>
|
<mysql-connector-j.version>8.3.0</mysql-connector-j.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
Loading…
Reference in New Issue