Created
November 8, 2016 10:58
-
-
Save qwwdfsad/e492848d03702b3d1896067110aeaa31 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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