我有一个程序,它读取一个包含指定速率的400万行的巨大文件,并将这些行发送到Kafka集群。这是该程序的代码:
try (BufferedReader br = new BufferedReader(new FileReader(path.toString()))) {
String line = null;
do {
long start = System.currentTimeMillis();
// rate is is the number of messages per second
for (int i = 0; i < rate; i++) {
line = br.readLine();
if (line != null) {
// asynchronously send to the Kafka cluster
producer.send(new ProducerRecord<>(Config.KAFKA_TOPIC, Integer.toString(getNextInt()), line));
} else break;
}
long finish = System.currentTimeMillis() - start;
long sleepTime = 1000 - finish;
if (sleepTime > 0)
Thread.sleep(sleepTime);
else
System.out.println(sleepTime + " is negative");
} while (line != null);
br.close();
}
catch (InterruptedException | IOException | IllegalArgumentException ex) {
ex.printStackTrace();
}
在这种情况发生之前一切正常:
GC停止收集适量的对象,程序无法再保持读取率。我试图在System.gc()
之前调用Thread.sleep()
,但它始终不起作用。可能是什么问题?