Setの同時エラー

2478 ワード

package test;

import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class Main {

    private Set<Integer> set = Collections
            .synchronizedSet(new TreeSet<Integer>());

    public Main() {
        super();
    }

    public void print() {
        // System.out.println("print:"+Thread.currentThread().getName());
        Iterator<Integer> it = set.iterator();

        while (it.hasNext()) {
            Integer num = it.next();
            System.out.print(num);
        }

    }

    public void add(int i) {
        // System.out.println("add:"+Thread.currentThread().getName());
        set.add(i);

    }

    public void remove(int i) {
        // System.out.println("rempove:"+Thread.currentThread().getName());
        if (set.contains(i)) {
            set.remove(i);
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {

        final Main main = new Main();

        Runnable run1 = new Runnable() {

            @Override
            public void run() {

                while (true) {
                    main.print();
                    System.out.println("");
                }
            }
        };

        Runnable run2 = new Runnable() {

            @Override
            public void run() {

                while (true) {

                    for (int i = 0; i < 10; ++i) {
                        main.add(i);
                    }
                }
            }
        };

        Runnable run3 = new Runnable() {

            @Override
            public void run() {

                while (true) {

                    for (int i = 0; i < 10; ++i) {
                        main.remove(i);
                    }
                }
            }
        };

        new Thread(run1).start();
        new Thread(run2).start();
        new Thread(run3).start();
    }

}