Java 用多线程读取文件

Java 用多线程读取文件,java,multithreading,Java,Multithreading,是否可以通过运行多个线程来读取文本文件,以便接收到的行包含有关读取此行的线程的信息? 现在,我可以用一个线程来阅读: public class Test { public static void main(String[] args) throws InterruptedException { Deque<String> deque = new LinkedList<>(); for (int i = 0; i < 4; i

是否可以通过运行多个线程来读取文本文件,以便接收到的行包含有关读取此行的线程的信息? 现在,我可以用一个线程来阅读:

public class Test {

    public static void main(String[] args) throws InterruptedException {
        Deque<String> deque = new LinkedList<>();
        for (int i = 0; i < 4; i++) {
            new Thread(new SubReadThread(deque)).start();
        }
        new Thread(new WriteThread(deque)).start();
    }

}

class SubReadThread implements Runnable {
    private final Deque<String> deque;

    public SubReadThread(Deque<String> deque) {
        this.deque = deque;
    }

    @Override
    public void run() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new     FileInputStream("list.txt"), "UTF8"));
            String line = null;
            String newLine;
            while (true) {

                synchronized (deque) {
                    if (deque.size() < 1) {
                        line = br.readLine();
                        newLine = "#" + (Thread.currentThread().getId() - 9) + " " + line;
                        deque.addLast(newLine);
                        deque.notify();
                    } else {
                        deque.wait();
                    }
                    if (line == null) {
                        break;
                    }
                }
            }

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class WriteThread implements Runnable {
    private final Deque<String> deque;
    private List<String> list = new ArrayList<>();

    public WriteThread(Deque<String> deque) {
        this.deque = deque;
    }

    @Override
    public void run() {
        String line;

        while (true) {
            synchronized (deque) {
                if (deque.size() > 0) {
                    if ((line = deque.pollFirst()).contains("null")) {
                        break;
                    } else {
                        list.add(line);
                        deque.notifyAll();
                    }
                } else {
                    try {
                        deque.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        for(String s : list) {
            System.out.println(s);
        }
    }
}
公共类测试{
公共静态void main(字符串[]args)引发InterruptedException{
Deque Deque=新链接列表();
对于(int i=0;i<4;i++){
新线程(新子线程(deque)).start();
}
新线程(新的WriteThread(deque)).start();
}
}
类SubReadThread实现可运行{
私人决赛;
公共子读取线程(Deque Deque){
this.deque=deque;
}
@凌驾
公开募捐{
试一试{
BufferedReader br=新的BufferedReader(新的InputStreamReader(新文件InputStream(“list.txt”),“UTF8”);
字符串行=null;
字符串换行符;
while(true){
已同步(deque){
if(deque.size()<1){
line=br.readLine();
换行符=“#”+(Thread.currentThread().getId()-9)+”行;
deque.addLast(换行符);
deque.notify();
}否则{
deque.wait();
}
如果(行==null){
打破
}
}
}
}捕获(IOException | InterruptedException e){
e、 printStackTrace();
}
}
}
类WriteThread实现可运行{
私人决赛;
私有列表=新的ArrayList();
公开阅读(Deque Deque){
this.deque=deque;
}
@凌驾
公开募捐{
弦线;
while(true){
已同步(deque){
如果(deque.size()>0){
如果((line=deque.pollFirst()).contains(“null”)){
打破
}否则{
列表。添加(行);
deque.notifyAll();
}
}否则{
试一试{
deque.wait();
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}
}
用于(字符串s:列表){
系统输出打印项次;
}
}
}
预期产出如下:

#3第1行

#第1行第2行

#4第3行

#2第4行


更新正常工作所需的所有操作,将BufferedReader移动到main方法并将其对象传递给构造函数。

是的,但由于多个线程试图从单个主轴读取数据,因此速度会变慢。可以使用多个线程并行读取文件。您是在尝试将同一个文件完全读取n次,还是使用每个块一个线程(分治)将一个大文件并行分块以提高速度?这里的文件是一个瓶颈,您可能需要同步线程以正确读取数据。这样做的目的是什么?检查这个->它不是更简单吗?Files.lines(path.get(“myFilePath”)).parallel()但如果是为了体验,我认为使用RandomAccessFile可以更好地实现这一点,也就是说,每10%的字节使用不同的线程,因此您不必担心同步块,因为您知道每个线程将访问其自己的数据部分