Skip to content

Instantly share code, notes, and snippets.

@qwwdfsad
Created November 8, 2016 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qwwdfsad/e492848d03702b3d1896067110aeaa31 to your computer and use it in GitHub Desktop.
Save qwwdfsad/e492848d03702b3d1896067110aeaa31 to your computer and use it in GitHub Desktop.
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
public class Main {
volatile Object consumer;
volatile List<String> list;
private void run() throws Exception {
list = Arrays.asList("true", "true", "true");
Supplier<Void> f1 = () -> {
exceptionalParse();
return null;
};
Supplier<Void> f2 = () -> {
iterate();
return null;
};
Supplier<Void> f3 = () -> {
iterate();
return null;
};
Supplier<Void> f4 = () -> {
iterate();
return null;
};
Supplier<Void>[] arr = new Supplier[]{f1, f2, f3, f4};
for (Supplier<Void> supplier : arr) {
new Thread(() -> {
while (true) {
depth(20, supplier);
}
}).start();
}
Thread.sleep(TimeUnit.MINUTES.toMillis(5));
}
private void exceptionalParse() {
double sum = 0.0;
for (String s : list) {
try {
sum += Double.parseDouble(s);
}
catch (NumberFormatException e) {
}
}
consumer = sum;
if (consumer == null) {
throw new NullPointerException();
}
}
private void iterate() {
int size = 0;
for (String s : list) {
++size;
}
consumer = size;
if (consumer == null) {
throw new NullPointerException();
}
}
private void depth(int n, Supplier<Void> supplier) {
if (--n != 0) {
depth(n, supplier);
}
else {
supplier.get();
}
}
public static void main(String[] args) throws Exception {
new Main().run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment