jeudi 19 octobre 2023

Why do I need the "Iterator pattern" for parallel iteration over a collection?

E.g. the following code allows parall iteration over a list without throwing an exception. So why do I need the Iterator pattern for parallel iteration?

    public static void main(String[] args) {

    List<Integer> myList = Lists.newArrayList();
    for (int i = 0; i < 1000; i++) {
        myList.add(i);
    }

    new Thread(new Runnable() {
        @Override public void run() {
            for (final Integer integer : myList) {
                System.out.println("Thread-A");
            }
        }
    }).start();

    new Thread(new Runnable() {
        @Override public void run() {
            for (final Integer integer : myList) {
                System.out.println("Thread-B");
            }
        }
    }).start();
}

Aucun commentaire:

Enregistrer un commentaire