import java.util.*;

/**
 * Created by stud on 16.04.2015.
 */
public class SyncronizeSample {
    private static final  List<Object> monitors = new ArrayList<Object>();
    private static final Object monitor = new Object();
    private static Map<String, Integer> counter = Collections.synchronizedMap(new HashMap<String, Integer>());



    public static void main(String[] args) throws InterruptedException{
Long start=System.currentTimeMillis();
        List<Thread> threads = new ArrayList<Thread>();

        for (int i=0; i<100; i++){
            final int threadNum=i;
            Thread thread = new Thread(() -> incrementCounter(threadNum));
            thread.start();
            threads.add(thread);
        }
        for(Thread t:threads){
            t.join();
        }
        System.out.println((System.currentTimeMillis()-start));
        System.out.println(new TreeMap<String, Integer>(counter));
        System.out.println(counter.size());
    }

    public static synchronized void incrementCounter(int threadNumber) {

            System.out.println("start = " + threadNumber);
            for (int i = 0; i<10000; i++){
            synchronized (monitors.get(i)) {
                Integer count = null;
                count = counter.get(String.valueOf(i));

                if (count == null) {
                    count = 0;
                }
                counter.put(String.valueOf(i), count + 1);
            }
            }
            System.out.println("End = " + threadNumber);
        }
public void doSmth(){
    synchronized (monitor){

    }
}
}
