wait(), notify() - 用于从文件快速读取



我不明白为什么我从一行上的文件读取的方法工作得这么慢。

下面是我的课程示例。它的速度为10MB/14秒。 同时,主要的延迟是由 wait(( 和 notify(( 引起的。为什么会这样?

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class Reader {
public static final String fileEnd = "=-=-=-=END=-=-=-=";

private String nextLine;
public Reader(File file){
consume(file);
}
public String getLine(){
String line;
synchronized (this){
while (true) {
if(nextLine == null){
notify();

try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else if(nextLine.equals(fileEnd)){
return fileEnd;
}
else {
line = nextLine;
nextLine = null;
notify();
break;
}
}
}
return line;
}
private void consume(File file){
Thread thread = new Thread(()->{
final char c = System.getProperty("line.separator").charAt(0);
try (RandomAccessFile aFile = new RandomAccessFile(file, "r")){
FileChannel inChannel = aFile.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(10240);
StringBuilder line = new StringBuilder();
while (inChannel.read(buffer) > 0) {
buffer.flip();

for (int i = 0; i < buffer.limit(); i++) {
char as = (char) buffer.get();
if(as==c){
String compliteLine = line.toString();
line = new StringBuilder();
compliteLine = compliteLine.replace("r", "").replace("n", "");

synchronized (this){
while (true) {
if (nextLine == null) {
nextLine = compliteLine;

try {
notify();
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
} else {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
else {
line.append(as);
}
}
buffer.clear();
}
inChannel.close();
synchronized (this){
notify();
wait();
nextLine = fileEnd;
notify();
}
}
catch (Exception ignored){ignored.printStackTrace();}
});
thread.setPriority(Thread.MAX_PRIORITY);
thread.setDaemon(true);
thread.start();
}
}

在此处输入图像描述

我还尝试在队列为 100 的 ArrayBlockingQueue 上做同样的事情。 读取同一文件需要不到一秒钟的时间。 但是这个弗塞拉夫诺非常缓慢。 上面的代码是我自己想出来的,为了加快阅读速度,但它发生得更慢。

问题本身就是为什么代码比队列更高,更慢。 以及如何加速它?

在上面的代码中,Thread 在读取的每一行上都阻塞。使用ArrayBlockingQueue,线程只有在从队列中删除之前从文件中读取了100个项目时才会阻塞。根据将项目从队列中删除的处理速度,它可能永远不会阻止。

如果你想使上面的代码更快,你可以考虑使用多个缓冲区,这样 Thread 就不需要阻塞每一行。从本质上讲,您将自己实现BlockingQueue

谢谢大家的帮助。 我被LinkedTransferQueue救了出来。 它完全按照我的需要工作。

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.concurrent.LinkedTransferQueue;
public class LinkedTransferQueueReader {
public static final String fileEnd = "=-=-=-=END=-=-=-=";

private LinkedTransferQueue<String> queue = new LinkedTransferQueue<>();

public LinkedTransferQueueReader(File file){
consume(file);
}

public String getLine(){
String line = "";

try {
line = queue.take();
} catch (InterruptedException e) {
e.printStackTrace();
}

return line;
}
private void consume(File file){
Thread thread = new Thread(()->{
final String lineSeparator = System.getProperty("line.separator");
final char r = lineSeparator.charAt(0);
char n = '';
if(lineSeparator.length() > 1)
n = lineSeparator.charAt(1);
try (RandomAccessFile aFile = new RandomAccessFile(file, "r")){
FileChannel inChannel = aFile.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(10240);
StringBuilder line = new StringBuilder();
while (inChannel.read(buffer) > 0) {
buffer.flip();

for (int i = 0; i < buffer.limit(); i++) {
char as = (char) buffer.get();
if(as == n)
continue;
if(as==r){
String compliteLine = line.toString();
line = new StringBuilder();

queue.transfer(compliteLine);
}
else {
line.append(as);
}
}
buffer.clear();
}
inChannel.close();
queue.transfer(fileEnd);
}
catch (Exception ignored){ignored.printStackTrace();}
});
thread.setPriority(Thread.MAX_PRIORITY);
thread.setDaemon(true);
thread.start();
}
}

最新更新