共计 16067 个字符,预计需要花费 41 分钟才能阅读完成。
场景阐明
现有一个 10G 文件的数据,外面蕴含了 18-70 之间的整数,别离示意 18-70 岁的人群数量统计,假如年龄范畴散布平均,别离示意零碎中所有用户的年龄数,找出反复次数最多的那个数,现有一台内存为 4G、2 核 CPU 的电脑,请写一个算法实现。
23,31,42,19,60,30,36,........
模仿数据
Java 中一个整数占 4 个字节,模仿 10G 为 30 亿左右个数据,采纳追加模式写入 10G 数据到硬盘里。每 100 万个记录写一行,大略 4M 一行,10G 大略 2500 行数据。
package bigdata; | |
import java.io.*; | |
import java.util.Random; | |
/** | |
* @Desc: | |
* @Author: bingbing | |
* @Date: 2022/5/4 0004 19:05 | |
*/ | |
public class GenerateData {private static Random random = new Random(); | |
public static int generateRandomData(int start, int end) {return random.nextInt(end - start + 1) + start; | |
} | |
/** | |
* 产生 10G 的 1-1000 的数据在 D 盘 | |
*/ | |
public void generateData() throws IOException {File file = new File("D:\ User.dat"); | |
if (!file.exists()) { | |
try {file.createNewFile(); | |
} catch (IOException e) {e.printStackTrace(); | |
} | |
} | |
int start = 18; | |
int end = 70; | |
long startTime = System.currentTimeMillis(); | |
BufferedWriter bos = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true))); | |
for (long i = 1; i < Integer.MAX_VALUE * 1.7; i++) {String data = generateRandomData(start, end) + ","; | |
bos.write(data); | |
// 每 100 万条记录成一行,100 万条数据大略 4M | |
if (i % 1000000 == 0) {bos.write("\n"); | |
} | |
} | |
System.out.println("写入实现! 共破费工夫:" + (System.currentTimeMillis() - startTime) / 1000 + "s"); | |
bos.close();} | |
public static void main(String[] args) {GenerateData generateData = new GenerateData(); | |
try {generateData.generateData(); | |
} catch (IOException e) {e.printStackTrace(); | |
} | |
} | |
} |
上述代码调整参数执行 2 次,凑 10 个 G 的数据在 D 盘的 User.dat 文件里。
筹备好 10G 数据后,接着写如何解决这些数据。
场景剖析
10G 的数据比以后领有的运行内存大的多,不能全量加载到内存中读取,如果采纳全量加载,那么内存会间接爆掉,只能按行读取,Java 中的 bufferedReader 的 readLine() 按行读取文件里的内容。
读取数据
首先咱们写一个办法单线程读完这 30E 数据须要多少工夫,每读 100 行打印一次:
private static void readData() throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(FILE_NAME), "utf-8")); | |
String line; | |
long start = System.currentTimeMillis(); | |
int count = 1; | |
while ((line = br.readLine()) != null) { | |
// 按行读取 | |
// SplitData.splitLine(line); | |
if (count % 100 == 0) {System.out.println("读取 100 行, 总耗工夫:" + (System.currentTimeMillis() - start) / 1000 + "s"); | |
System.gc();} | |
count++; | |
} | |
running = false; | |
br.close();} |
按行读完 10G 的数据大略 20 秒,根本每 100 行,1E 多数据花 1S,速度还挺快:
解决数据
| 思路一:通过单线程解决
通过单线程解决,初始化一个 countMap,key 为年龄,value 为呈现的次数,将每行读取到的数据依照 “,” 进行宰割,而后获取到的每一项进行保留到 countMap 里,如果存在,那么值 key 的 value+1。
for (int i = start; i <= end; i++) { | |
try {File subFile = new File(dir + "\" + i + ".dat"); | |
if (!file.exists()) {subFile.createNewFile(); | |
} | |
countMap.computeIfAbsent(i + "", integer -> new AtomicInteger(0)); | |
} catch (FileNotFoundException e) {e.printStackTrace(); | |
} catch (IOException e) {e.printStackTrace(); | |
} | |
} |
单线程读取并统计 countMap:
public static void splitLine(String lineData) {String[] arr = lineData.split(","); | |
for (String str : arr) {if (StringUtils.isEmpty(str)) {continue;} | |
countMap.computeIfAbsent(str, s -> new AtomicInteger(0)).getAndIncrement();} | |
} |
通过比拟找出年龄数最多的年龄并打印进去:
private static void findMostAge() { | |
Integer targetValue = 0; | |
String targetKey = null; | |
Iterator<Map.Entry<String, AtomicInteger>> entrySetIterator = countMap.entrySet().iterator(); | |
while (entrySetIterator.hasNext()) {Map.Entry<String, AtomicInteger> entry = entrySetIterator.next(); | |
Integer value = entry.getValue().get(); | |
String key = entry.getKey(); | |
if (value > targetValue) { | |
targetValue = value; | |
targetKey = key; | |
} | |
} | |
System.out.println("数量最多的年龄为:" + targetKey + "数量为:" + targetValue); | |
} |
残缺代码:
package bigdata; | |
import org.apache.commons.lang3.StringUtils; | |
import java.io.*; | |
import java.util.*; | |
import java.util.concurrent.ConcurrentHashMap; | |
import java.util.concurrent.atomic.AtomicInteger; | |
/** | |
* @Desc: | |
* @Author: bingbing | |
* @Date: 2022/5/4 0004 19:19 | |
* 单线程解决 | |
*/ | |
public class HandleMaxRepeatProblem_v0 { | |
public static final int start = 18; | |
public static final int end = 70; | |
public static final String dir = "D:\dataDir"; | |
public static final String FILE_NAME = "D:\ User.dat"; | |
/** | |
* 统计数量 | |
*/ | |
private static Map<String, AtomicInteger> countMap = new ConcurrentHashMap<>(); | |
/** | |
* 开启生产的标记 | |
*/ | |
private static volatile boolean startConsumer = false; | |
/** | |
* 消费者运行保障 | |
*/ | |
private static volatile boolean consumerRunning = true; | |
/** | |
* 依照 "," 宰割数据,并写入到 countMap 里 | |
*/ | |
static class SplitData {public static void splitLine(String lineData) {String[] arr = lineData.split(","); | |
for (String str : arr) {if (StringUtils.isEmpty(str)) {continue;} | |
countMap.computeIfAbsent(str, s -> new AtomicInteger(0)).getAndIncrement();} | |
} | |
} | |
/** | |
* init map | |
*/ | |
static {File file = new File(dir); | |
if (!file.exists()) {file.mkdir(); | |
} | |
for (int i = start; i <= end; i++) { | |
try {File subFile = new File(dir + "\" + i + ".dat"); | |
if (!file.exists()) {subFile.createNewFile(); | |
} | |
countMap.computeIfAbsent(i + "", integer -> new AtomicInteger(0)); | |
} catch (FileNotFoundException e) {e.printStackTrace(); | |
} catch (IOException e) {e.printStackTrace(); | |
} | |
} | |
} | |
public static void main(String[] args) {new Thread(() -> { | |
try {readData(); | |
} catch (IOException e) {e.printStackTrace(); | |
} | |
}).start();} | |
private static void readData() throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(FILE_NAME), "utf-8")); | |
String line; | |
long start = System.currentTimeMillis(); | |
int count = 1; | |
while ((line = br.readLine()) != null) { | |
// 按行读取,并向 map 里写入数据 | |
SplitData.splitLine(line); | |
if (count % 100 == 0) {System.out.println("读取 100 行, 总耗工夫:" + (System.currentTimeMillis() - start) / 1000 + "s"); | |
try {Thread.sleep(1000L); | |
} catch (InterruptedException e) {e.printStackTrace(); | |
} | |
} | |
count++; | |
} | |
findMostAge(); | |
br.close();} | |
private static void findMostAge() { | |
Integer targetValue = 0; | |
String targetKey = null; | |
Iterator<Map.Entry<String, AtomicInteger>> entrySetIterator = countMap.entrySet().iterator(); | |
while (entrySetIterator.hasNext()) {Map.Entry<String, AtomicInteger> entry = entrySetIterator.next(); | |
Integer value = entry.getValue().get(); | |
String key = entry.getKey(); | |
if (value > targetValue) { | |
targetValue = value; | |
targetKey = key; | |
} | |
} | |
System.out.println("数量最多的年龄为:" + targetKey + "数量为:" + targetValue); | |
} | |
private static void clearTask() { | |
// 清理,同时找出呈现字符最大的数 | |
findMostAge(); | |
System.exit(-1); | |
} | |
} |
测试后果: 总共花了 3 分钟读取完并统计完所有数据。
内存耗费为 2G-2.5G,CPU 利用率太低,只向上浮动了 20%-25% 之间:
要想进步 CPU 的利用率,那么能够应用多线程去解决。上面咱们应用多线程去解决这个 CPU 利用率低的问题。
| 思路二:分治法
应用多线程去生产读取到的数据。采纳生产者、消费者模式去生产数据,因为在读取的时候是比拟快的,单线程的数据处理能力比拟差,因而思路一的性能阻塞在取数据方,又是同步的,所以导致整个链路的性能会变的很差。
所谓分治法就是分而治之,也就是说将海量数据宰割解决。依据 CPU 的能力初始化 n 个线程,每一个线程去生产一个队列,这样线程在生产的时候不会呈现抢占队列的问题。
同时为了保障线程平安和生产者消费者模式的残缺,采纳阻塞队列,Java 中提供了 LinkedBlockingQueue 就是一个阻塞队列。
①初始化阻塞队列
应用 linkedList 创立一个阻塞队列列表:
private static List<LinkedBlockingQueue<String>> blockQueueLists = new LinkedList<>();
在 static 块里初始化阻塞队列的数量和单个阻塞队列的容量为 256,下面讲到了 30E 数据大略 2500 行,按行塞到队列里,20 个队列,那么每个队列 125 个,因而能够容量能够设计为 256 即可:
// 每个队列容量为 256 | |
for (int i = 0; i < threadNums; i++) {blockQueueLists.add(new LinkedBlockingQueue<>(256)); | |
} |
②生产者
为了实现负载的性能, 首先定义一个 count 计数器,用来记录行数:
private static AtomicLong count = new AtomicLong(0);
依照行数来计算队列的下标:long index=count.get()%threadNums。
上面算法就实现了对队列列表中的队列进行轮询的投放:
static class SplitData {public static void splitLine(String lineData) {// System.out.println(lineData.length()); | |
String[] arr = lineData.split("\n"); | |
for (String str : arr) {if (StringUtils.isEmpty(str)) {continue;} | |
long index = count.get() % threadNums; | |
try { | |
// 如果满了就阻塞 | |
blockQueueLists.get((int) index).put(str); | |
} catch (InterruptedException e) {e.printStackTrace(); | |
} | |
count.getAndIncrement();} | |
} |
③消费者
队列线程私有化:生产方在启动线程的时候依据 index 去获取到指定的队列,这样就实现了队列的线程私有化。
private static void startConsumer() throws FileNotFoundException, UnsupportedEncodingException { | |
// 如果共用一个队列,那么线程不宜过多,容易呈现抢占景象 | |
System.out.println("开始生产..."); | |
for (int i = 0; i < threadNums; i++) { | |
final int index = i; | |
// 每一个线程负责一个 queue,这样不会呈现线程抢占队列的状况。new Thread(() -> {while (consumerRunning) { | |
startConsumer = true; | |
try {String str = blockQueueLists.get(index).take(); | |
countNum(str); | |
} catch (InterruptedException e) {e.printStackTrace(); | |
} | |
} | |
}).start();} | |
} |
多子线程宰割字符串:因为从队列中多到的字符串十分的宏大,如果又是用单线程调用 split(“,”) 去宰割,那么性能同样会阻塞在这个中央。
// 依照 arr 的大小,使用多线程宰割字符串 | |
private static void countNum(String str) {int[] arr = new int[2]; | |
arr[1] = str.length() / 3; | |
// System.out.println("宰割的字符串为 start 地位为:" + arr[0] + ",end 地位为:" + arr[1]); | |
for (int i = 0; i < 3; i++) {final String innerStr = SplitData.splitStr(str, arr); | |
// System.out.println("宰割的字符串为 start 地位为:" + arr[0] + ",end 地位为:" + arr[1]); | |
new Thread(() -> {String[] strArray = innerStr.split(","); | |
for (String s : strArray) {countMap.computeIfAbsent(s, s1 -> new AtomicInteger(0)).getAndIncrement();} | |
}).start();} | |
} |
宰割字符串算法:宰割时从 0 开始,依照等分的准则,将字符串 n 等份,每一个线程分到一份。
用一个 arr 数组的 arr[0] 记录每次的宰割开始地位,arr[1] 记录每次宰割的完结地位,如果遇到的开始的字符不为 “,”,那么就 startIndex-1,如果完结的地位不为 “,”,那么将 endIndex 向后移一位。
如果 endIndex 超过了字符串的最大长度,那么就把最初一个字符赋值给 arr[1]。
/** | |
* 依照 x 坐标 来宰割 字符串,如果切到的字符不为“,”,那么把坐标向前或者向后挪动一位。* | |
* @param line | |
* @param arr 寄存 x1,x2 坐标 | |
* @return | |
*/ | |
public static String splitStr(String line, int[] arr) {int startIndex = arr[0]; | |
int endIndex = arr[1]; | |
char start = line.charAt(startIndex); | |
char end = line.charAt(endIndex); | |
if ((startIndex == 0 || start == ',') && end == ',') {arr[0] = endIndex + 1; | |
arr[1] = arr[0] + line.length() / 3; | |
if (arr[1] >= line.length()) {arr[1] = line.length() - 1;} | |
return line.substring(startIndex, endIndex); | |
} | |
if (startIndex != 0 && start != ',') {startIndex = startIndex - 1;} | |
if (end != ',') {endIndex = endIndex + 1;} | |
arr[0] = startIndex; | |
arr[1] = endIndex; | |
if (arr[1] >= line.length()) {arr[1] = line.length() - 1;} | |
return splitStr(line, arr); | |
} |
残缺代码:
package bigdata; | |
import cn.hutool.core.collection.CollectionUtil; | |
import org.apache.commons.lang3.StringUtils; | |
import java.io.*; | |
import java.util.*; | |
import java.util.concurrent.ConcurrentHashMap; | |
import java.util.concurrent.LinkedBlockingQueue; | |
import java.util.concurrent.atomic.AtomicInteger; | |
import java.util.concurrent.atomic.AtomicLong; | |
import java.util.concurrent.locks.ReentrantLock; | |
/** | |
* @Desc: | |
* @Author: bingbing | |
* @Date: 2022/5/4 0004 19:19 | |
* 多线程解决 | |
*/ | |
public class HandleMaxRepeatProblem { | |
public static final int start = 18; | |
public static final int end = 70; | |
public static final String dir = "D:\dataDir"; | |
public static final String FILE_NAME = "D:\ User.dat"; | |
private static final int threadNums = 20; | |
/** | |
* key 为年龄,value 为所有的行列表,应用队列 | |
*/ | |
private static Map<Integer, Vector<String>> valueMap = new ConcurrentHashMap<>(); | |
/** | |
* 存放数据的队列 | |
*/ | |
private static List<LinkedBlockingQueue<String>> blockQueueLists = new LinkedList<>(); | |
/** | |
* 统计数量 | |
*/ | |
private static Map<String, AtomicInteger> countMap = new ConcurrentHashMap<>(); | |
private static Map<Integer, ReentrantLock> lockMap = new ConcurrentHashMap<>(); | |
// 队列负载平衡 | |
private static AtomicLong count = new AtomicLong(0); | |
/** | |
* 开启生产的标记 | |
*/ | |
private static volatile boolean startConsumer = false; | |
/** | |
* 消费者运行保障 | |
*/ | |
private static volatile boolean consumerRunning = true; | |
/** | |
* 依照 "," 宰割数据,并写入到文件里 | |
*/ | |
static class SplitData {public static void splitLine(String lineData) {// System.out.println(lineData.length()); | |
String[] arr = lineData.split("\n"); | |
for (String str : arr) {if (StringUtils.isEmpty(str)) {continue;} | |
long index = count.get() % threadNums; | |
try { | |
// 如果满了就阻塞 | |
blockQueueLists.get((int) index).put(str); | |
} catch (InterruptedException e) {e.printStackTrace(); | |
} | |
count.getAndIncrement();} | |
} | |
/** | |
* 依照 x 坐标 来宰割 字符串,如果切到的字符不为“,”,那么把坐标向前或者向后挪动一位。* | |
* @param line | |
* @param arr 寄存 x1,x2 坐标 | |
* @return | |
*/ | |
public static String splitStr(String line, int[] arr) {int startIndex = arr[0]; | |
int endIndex = arr[1]; | |
char start = line.charAt(startIndex); | |
char end = line.charAt(endIndex); | |
if ((startIndex == 0 || start == ',') && end == ',') {arr[0] = endIndex + 1; | |
arr[1] = arr[0] + line.length() / 3; | |
if (arr[1] >= line.length()) {arr[1] = line.length() - 1;} | |
return line.substring(startIndex, endIndex); | |
} | |
if (startIndex != 0 && start != ',') {startIndex = startIndex - 1;} | |
if (end != ',') {endIndex = endIndex + 1;} | |
arr[0] = startIndex; | |
arr[1] = endIndex; | |
if (arr[1] >= line.length()) {arr[1] = line.length() - 1;} | |
return splitStr(line, arr); | |
} | |
public static void splitLine0(String lineData) {String[] arr = lineData.split(","); | |
for (String str : arr) {if (StringUtils.isEmpty(str)) {continue;} | |
int keyIndex = Integer.parseInt(str); | |
ReentrantLock lock = lockMap.computeIfAbsent(keyIndex, lockMap -> new ReentrantLock()); | |
lock.lock(); | |
try {valueMap.get(keyIndex).add(str); | |
} finally {lock.unlock(); | |
} | |
// boolean wait = true; | |
// for (; ;) {// if (!lockMap.get(Integer.parseInt(str)).isLocked()) { | |
// wait = false; | |
// valueMap.computeIfAbsent(Integer.parseInt(str), integer -> new Vector<>()).add(str); | |
// } | |
// // 以后阻塞,直到开释锁 | |
// if (!wait) { | |
// break; | |
// } | |
// } | |
} | |
} | |
} | |
/** | |
* init map | |
*/ | |
static {File file = new File(dir); | |
if (!file.exists()) {file.mkdir(); | |
} | |
// 每个队列容量为 256 | |
for (int i = 0; i < threadNums; i++) {blockQueueLists.add(new LinkedBlockingQueue<>(256)); | |
} | |
for (int i = start; i <= end; i++) { | |
try {File subFile = new File(dir + "\" + i + ".dat"); | |
if (!file.exists()) {subFile.createNewFile(); | |
} | |
countMap.computeIfAbsent(i + "", integer -> new AtomicInteger(0)); | |
// lockMap.computeIfAbsent(i, lock -> new ReentrantLock()); | |
} catch (FileNotFoundException e) {e.printStackTrace(); | |
} catch (IOException e) {e.printStackTrace(); | |
} | |
} | |
} | |
public static void main(String[] args) {new Thread(() -> { | |
try { | |
// 读取数据 | |
readData();} catch (IOException e) {e.printStackTrace(); | |
} | |
}).start(); | |
new Thread(() -> { | |
try { | |
// 开始生产 | |
startConsumer();} catch (FileNotFoundException e) {e.printStackTrace(); | |
} catch (UnsupportedEncodingException e) {e.printStackTrace(); | |
} | |
}).start(); | |
new Thread(() -> { | |
// 监控 | |
monitor();}).start();} | |
/** | |
* 每隔 60s 去查看栈是否为空 | |
*/ | |
private static void monitor() {AtomicInteger emptyNum = new AtomicInteger(0); | |
while (consumerRunning) { | |
try {Thread.sleep(10 * 1000); | |
} catch (InterruptedException e) {e.printStackTrace(); | |
} | |
if (startConsumer) { | |
// 如果所有栈的大小都为 0,那么终止过程 | |
AtomicInteger emptyCount = new AtomicInteger(0); | |
for (int i = 0; i < threadNums; i++) {if (blockQueueLists.get(i).size() == 0) {emptyCount.getAndIncrement(); | |
} | |
} | |
if (emptyCount.get() == threadNums) {emptyNum.getAndIncrement(); | |
// 如果间断查看指定次数都为空,那么就进行生产 | |
if (emptyNum.get() > 12) { | |
consumerRunning = false; | |
System.out.println("生产完结..."); | |
try {clearTask(); | |
} catch (Exception e) {System.out.println(e.getCause()); | |
} finally {System.exit(-1); | |
} | |
} | |
} | |
} | |
} | |
} | |
private static void readData() throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(FILE_NAME), "utf-8")); | |
String line; | |
long start = System.currentTimeMillis(); | |
int count = 1; | |
while ((line = br.readLine()) != null) { | |
// 按行读取,并向队列写入数据 | |
SplitData.splitLine(line); | |
if (count % 100 == 0) {System.out.println("读取 100 行, 总耗工夫:" + (System.currentTimeMillis() - start) / 1000 + "s"); | |
try {Thread.sleep(1000L); | |
System.gc();} catch (InterruptedException e) {e.printStackTrace(); | |
} | |
} | |
count++; | |
} | |
br.close();} | |
private static void clearTask() { | |
// 清理,同时找出呈现字符最大的数 | |
Integer targetValue = 0; | |
String targetKey = null; | |
Iterator<Map.Entry<String, AtomicInteger>> entrySetIterator = countMap.entrySet().iterator(); | |
while (entrySetIterator.hasNext()) {Map.Entry<String, AtomicInteger> entry = entrySetIterator.next(); | |
Integer value = entry.getValue().get(); | |
String key = entry.getKey(); | |
if (value > targetValue) { | |
targetValue = value; | |
targetKey = key; | |
} | |
} | |
System.out.println("数量最多的年龄为:" + targetKey + "数量为:" + targetValue); | |
System.exit(-1); | |
} | |
/** | |
* 应用 linkedBlockQueue | |
* | |
* @throws FileNotFoundException | |
* @throws UnsupportedEncodingException | |
*/ | |
private static void startConsumer() throws FileNotFoundException, UnsupportedEncodingException { | |
// 如果共用一个队列,那么线程不宜过多,容易呈现抢占景象 | |
System.out.println("开始生产..."); | |
for (int i = 0; i < threadNums; i++) { | |
final int index = i; | |
// 每一个线程负责一个 queue,这样不会呈现线程抢占队列的状况。new Thread(() -> {while (consumerRunning) { | |
startConsumer = true; | |
try {String str = blockQueueLists.get(index).take(); | |
countNum(str); | |
} catch (InterruptedException e) {e.printStackTrace(); | |
} | |
} | |
}).start();} | |
} | |
// 依照 arr 的大小,使用多线程宰割字符串 | |
private static void countNum(String str) {int[] arr = new int[2]; | |
arr[1] = str.length() / 3; | |
// System.out.println("宰割的字符串为 start 地位为:" + arr[0] + ",end 地位为:" + arr[1]); | |
for (int i = 0; i < 3; i++) {final String innerStr = SplitData.splitStr(str, arr); | |
// System.out.println("宰割的字符串为 start 地位为:" + arr[0] + ",end 地位为:" + arr[1]); | |
new Thread(() -> {String[] strArray = innerStr.split(","); | |
for (String s : strArray) {countMap.computeIfAbsent(s, s1 -> new AtomicInteger(0)).getAndIncrement();} | |
}).start();} | |
} | |
/** | |
* 后盾线程去生产 map 里数据写入到各个文件里, 如果不生产,那么会将内存程爆 | |
*/ | |
private static void startConsumer0() throws FileNotFoundException, UnsupportedEncodingException {for (int i = start; i <= end; i++) { | |
final int index = i; | |
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dir + "\" + i + ".dat", false), "utf-8")); | |
new Thread(() -> { | |
int miss = 0; | |
int countIndex = 0; | |
while (true) { | |
// 每隔 100 万打印一次 | |
int count = countMap.get(index).get(); | |
if (count > 1000000 * countIndex) {System.out.println(index + "岁年龄的个数为:" + countMap.get(index).get()); | |
countIndex += 1; | |
} | |
if (miss > 1000) { | |
// 终止线程 | |
try {Thread.currentThread().interrupt(); | |
bw.close();} catch (IOException e) {}} | |
if (Thread.currentThread().isInterrupted()) {break;} | |
Vector<String> lines = valueMap.computeIfAbsent(index, vector -> new Vector<>()); | |
// 写入到文件里 | |
try {if (CollectionUtil.isEmpty(lines)) { | |
miss++; | |
Thread.sleep(1000); | |
} else { | |
// 100 个一批 | |
if (lines.size() < 1000) {Thread.sleep(1000); | |
continue; | |
} | |
// 1000 个的时候开始解决 | |
ReentrantLock lock = lockMap.computeIfAbsent(index, lockIndex -> new ReentrantLock()); | |
lock.lock(); | |
try {Iterator<String> iterator = lines.iterator(); | |
StringBuilder sb = new StringBuilder(); | |
while (iterator.hasNext()) {sb.append(iterator.next()); | |
countMap.get(index).addAndGet(1); | |
} | |
try {bw.write(sb.toString()); | |
bw.flush();} catch (IOException e) {e.printStackTrace(); | |
} | |
// 革除掉 vector | |
valueMap.put(index, new Vector<>()); | |
} finally {lock.unlock(); | |
} | |
} | |
} catch (InterruptedException e) {}} | |
}).start();} | |
} | |
} |
测试后果:
内存和 CPU 初始占用大小:
启动后,运行时稳固在 11.7,CPU 稳固利用在 90% 以上。
总耗时由 180S 缩减到 103S,效率晋升 75%,失去的后果也与单线程解决的统一!
遇到的问题
如果在运行了的时候,发现 GC 忽然罢工了,开始不工作了,有可能是 JVM 的堆中存在的垃圾太多,没回收导致内存的突增。
解决办法:在读取肯定数量后,能够让主线程暂停几秒,手动调用 GC。
提醒:本 demo 的线程创立都是手动创立的,理论开发中应用的是线程池!
原文链接:https://blog.csdn.net/qq_3303…
近期热文举荐:
1.1,000+ 道 Java 面试题及答案整顿 (2022 最新版)
2. 劲爆!Java 协程要来了。。。
3.Spring Boot 2.x 教程,太全了!
4. 别再写满屏的爆爆爆炸类了,试试装璜器模式,这才是优雅的形式!!
5.《Java 开发手册(嵩山版)》最新公布,速速下载!
感觉不错,别忘了顺手点赞 + 转发哦!