Skip to content

Instantly share code, notes, and snippets.

@thobson
Last active September 14, 2020 03:39
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 thobson/e4c54347fe4735fa35db to your computer and use it in GitHub Desktop.
Save thobson/e4c54347fe4735fa35db to your computer and use it in GitHub Desktop.
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);
}
}
@xiaozhiliaoo
Copy link

main method should be TwoStreamTest test = new TwoStreamTest();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment