Last active
September 14, 2020 03:39
-
-
Save thobson/e4c54347fe4735fa35db 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
public class TwoStreamTest { | |
public static void main(String args[]) throws Exception { | |
ThreadTest test = new ThreadTest(); | |
test.executeTasks(); | |
} | |
void executeTasks() throws Exception { | |
final List<Integer> firstRange = buildIntRange(); | |
final List<Integer> secondRange = buildIntRange(); | |
Runnable firstTask = () -> { | |
firstRange.parallelStream().forEach((number) -> { | |
try { | |
// do something slow | |
Thread.sleep(5); | |
} catch (InterruptedException e) { } | |
}); | |
}; | |
Runnable secondTask = () -> { | |
secondRange.parallelStream().forEach((number) -> { | |
try { | |
// do something slow | |
Thread.sleep(5); | |
} catch (InterruptedException e) { } | |
}); | |
}; | |
Thread t1 = new Thread(firstTask); | |
Thread t2 = new Thread(secondTask); | |
t1.start(); | |
t2.start(); | |
t1.join(); | |
t2.join(); | |
} | |
private List<Integer> buildIntRange() { | |
List<Integer> numbers = new ArrayList<>(5); | |
for (int i=0; i<60_000; i++) | |
numbers.add(i); | |
return Collections.unmodifiableList(numbers); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
main method should be TwoStreamTest test = new TwoStreamTest();