Created
April 6, 2014 16:41
-
-
Save bubuzzz/10008465 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package com.thenet.training.threads; | |
| import java.util.ArrayList; | |
| import java.util.Collections; | |
| import java.util.HashMap; | |
| import java.util.List; | |
| import java.util.Map; | |
| import java.util.concurrent.ConcurrentHashMap; | |
| import com.thenet.training.ass6.Person; | |
| public class MapCheck { | |
| public long getMax(List<MapThread> threads) { | |
| long max = 0; | |
| for (int i = 0; i < threads.size(); i++) { | |
| if (threads.get(i).time > max) { | |
| max = threads.get(i).time; | |
| } | |
| } | |
| return max; | |
| } | |
| public void runCWith3Threads() { | |
| System.out.println("WARM UP ...."); | |
| for (int i = 0; i < 500; i++) { | |
| Map<Integer, Person> shm = Collections.synchronizedMap(new HashMap<Integer, Person>()); | |
| Map<Integer, Person> chm = new ConcurrentHashMap<Integer, Person>(20000); | |
| test(shm, 2, 1000, "SynchronizedMap"); | |
| test(chm, 2, 1000, "ConcurrentMap" ); | |
| if (i % 100 == 0) { | |
| System.out.println( (i / 100) + " warm up"); | |
| } | |
| } | |
| System.out.println("TESTING ...."); | |
| for (int i = 0; i < 5; i++) { | |
| Map<Integer, Person> shm = Collections.synchronizedMap(new HashMap<Integer, Person>()); | |
| Map<Integer, Person> chm = new ConcurrentHashMap<Integer, Person>(100000); | |
| System.out.println(test(shm, 4, 10000 * (i + 1), "SynchronizedMap")); | |
| System.out.println(test(chm, 4, 10000 * (i + 1), "ConcurrentMap" )); | |
| System.out.println(); | |
| } | |
| } | |
| private String test(Map<Integer, Person> map, int threadNo, int objNum, String type) { | |
| List<MapThread> threads = new ArrayList<MapThread>(); | |
| for (int t = 1; t <= threadNo ; t++) { | |
| threads.add(new MapThread(map, objNum * (t - 1), objNum * t)); | |
| } | |
| for (int t = 0; t < threadNo; t++) { | |
| threads.get(t).start(); | |
| } | |
| while (true) { | |
| try { | |
| for (int t = 0; t < threadNo; t++) { | |
| threads.get(t).join(); | |
| } | |
| break; | |
| } catch (InterruptedException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| return threadNo + " threads for " + objNum + " objects of " + type + " cost: " + getMax(threads); | |
| } | |
| public class MapThread extends Thread { | |
| Map<Integer, Person> map; | |
| int from; | |
| int to; | |
| long time; | |
| public MapThread(Map<Integer, Person> map, int from, int to) { | |
| this.map = map; | |
| this.from = from; | |
| this.to = to; | |
| } | |
| public void run() { | |
| long start = System.nanoTime(); | |
| for (int i = from; i < to; i++) { | |
| map.put(i, new Person()); | |
| } | |
| long end = System.nanoTime(); | |
| time = (end - start) / 1000000; | |
| return; | |
| } | |
| } | |
| public static void main(String[] args) { | |
| MapCheck check = new MapCheck(); | |
| check.runCWith3Threads(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment