Last active
December 12, 2016 19:10
-
-
Save apivovarov/db76dc9d29bd821b9189057db2ea613d to your computer and use it in GitHub Desktop.
This file contains 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 aaa; | |
import java.util.HashMap; | |
/** | |
* Use -Xmx30G -Xms30G for 100,000,000 rows | |
*/ | |
public class MapUpdater2 { | |
public static void main(String[] args) throws InterruptedException { | |
if (args.length != 3) { | |
throw new RuntimeException("Usage: MapUpdater2 <map_size_mil> <update_count_mil> <thread_count>"); | |
} | |
int mapSize = Integer.parseInt(args[0]) * 1000000; | |
int updCnt = Integer.parseInt(args[1]) * 1000000; | |
int thCnt = Integer.parseInt(args[2]); | |
HashMap<String, Row> map = new HashMap<>((int) (mapSize * 1.1)); | |
for (int i = 0; i < mapSize; i++) { | |
String dom = getDomain(i); | |
map.put(dom, new Row()); | |
} | |
System.out.println("Map Init Done"); | |
update(map, updCnt, thCnt); | |
update(map, updCnt, thCnt); | |
update(map, updCnt, thCnt); | |
update(map, updCnt, thCnt); | |
} | |
static void update(HashMap<String, Row> map, int updCnt, int thCnt) throws InterruptedException { | |
Thread[] tt = new Thread[thCnt]; | |
for (int i = 0; i < thCnt; i++) { | |
tt[i] = new Thread(new Updater(map, updCnt, thCnt, i)); | |
} | |
long s1 = System.currentTimeMillis(); | |
for (Thread t : tt) { | |
t.start(); | |
} | |
for (Thread t : tt) { | |
t.join(); | |
} | |
long s2 = System.currentTimeMillis(); | |
System.out.println(s2 - s1); | |
} | |
static class Row { | |
public long v1; | |
@Override | |
public String toString() { | |
return "row.v1: " + v1; | |
} | |
} | |
static String getDomain(int i) { | |
if (i < 10) { | |
return "d00000000" + i; | |
} else if (i < 100) { | |
return "d0000000" + i; | |
} else if (i < 1000) { | |
return "d000000" + i; | |
} else if (i < 10000) { | |
return "d00000" + i; | |
} else if (i < 100000) { | |
return "d0000" + i; | |
} else if (i < 1000000) { | |
return "d000" + i; | |
} else if (i < 10000000) { | |
return "d00" + i; | |
} else if (i < 100000000) { | |
return "d0" + i; | |
} else { | |
return "d" + i; | |
} | |
} | |
static class Updater implements Runnable { | |
HashMap<String, Row> map; | |
int updCnt; | |
int thCnt; | |
int part; | |
public Updater(HashMap<String, Row> map, int updCnt, int thCnt, int part) { | |
this.map = map; | |
this.updCnt = updCnt; | |
this.thCnt = thCnt; | |
this.part = part; | |
} | |
@Override | |
public void run() { | |
update(); | |
} | |
void update() { | |
for (int i = 0; i < updCnt; i++) { | |
if (i % thCnt != part) continue; | |
String dom = getDomain(i); | |
Row row = map.get(dom); | |
row.v1 += 1L; | |
} | |
} | |
} | |
} | |
// Map Init Done | |
// 133 | |
// 83 | |
// 66 | |
// 66 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment