【问题标题】:Write data into text file with multiple threads (simultaneously, in different lines of file)使用多个线程将数据写入文本文件(同时,在不同的文件行中)
【发布时间】:2020-11-25 20:53:23
【问题描述】:

创建k个线程同时将字符写入同一个文件:

  • 第一个线程在文件的第一行准确地写入一个数字 0 20 次;
  • 第二个线程在文件的第二行写入一个数字 1 正好 20 次;

...

  • 第十个线程在文件的第 10 行写入一个数字 9 正好 20 次;

实施要求。

  1. 每个数字的写入需要设置1毫秒的暂停。

  2. 使用 RandomAccessFile 将数据写入文件。

  3. 您只能使用 RandomAccessFile 类的一个对象!

我是这样写的:

import java.io.IOException;
import java.io.RandomAccessFile;

public class Part5 {
    // creates string before writing to the file
    public static String createString(int integer){
        StringBuilder result = new StringBuilder("");

        for(int i = 0; i < 20; i++){
            result.append(integer);
        }

        result.append("\n");
        return result.toString();
    }
    // writes string into the file
    public static void writeString(String st) {
        try(RandomAccessFile file = new RandomAccessFile("part5.txt", "rw")){
            st+="\n";
            file.write(st.getBytes());
        }catch(IOException ex){
            ex.getMessage();
        }
    }
    // starts writing threads
    public static void startThread(int number){
        Thread thread = new Thread(){
            @Override
            public void run() {
                synchronized (this){
                    writeString(createString(number));
                }
            }
        };

        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public static void main(final String[] args) {
        for(int i = 0; i < 9; i++){
            startThread(i);
        }
    }
}

我的实现只重写文件的第一行,但应该这样写:

00000000000000000000

11111111111111111111 

22222222222222222222 

33333333333333333333 

44444444444444444444 

55555555555555555555 

66666666666666666666 

77777777777777777777 

88888888888888888888 

99999999999999999999

如何修复代码的“并发部分”以使其正常工作? 提前谢谢!

【问题讨论】:

  • 这些要求似乎直接矛盾。如果您必须“使用 RandomAccessFile 将数据写入文件”并且还必须“使用不超过一个 RandomAccessFile 类的对象”,那么线程如何“同时将字符写入同一个文件”?只有一个类的实例,你只有一个文件指针。那么两个线程如何用一个文件指针同时写入呢?

标签: java multithreading concurrency java-io


【解决方案1】:

当您写入文件时,您需要将指针移动到正确的位置。您需要在 RandomAccessFile 中调用“seek”方法,然后将指针移动字节数。例如,第一个线程将寻找 0,第二个线程将寻找 21,依此类推。

您的程序现在的方式是,每个线程都会覆盖所有其他线程。

并行化也可能存在问题。

我不想给出现成的解决方案,但我很好奇。所以这里有一些你可以学习的东西

import java.io.IOException;
import java.io.RandomAccessFile;

public class Blah {
    // creates string before writing to the file
    public static String createString(int integer){
        StringBuilder result = new StringBuilder("");

        for(int i = 0; i < 20; i++){
            result.append(integer);
        }

        result.append("\n");
        return result.toString();
    }

    public static void main(final String[] args) throws IOException {
        RandomAccessFile file = new RandomAccessFile("part5.txt", "rw");
        Blah blah = new Blah();
        for(int i = 0; i <= 9; i++){
            Thread thread = new Thread(blah.new NumberWriter(i, file));
            thread.start();
        }

        file.close();
    }

    private class NumberWriter implements Runnable {
        int number;
        RandomAccessFile file;

        public NumberWriter(int number, RandomAccessFile file) {
            this.number = number;
            this.file = file;
        }


        public void run() {
            try {
                int seek = this.number * 20 + number;
                System.out.println("number is  : " + number + " : seeking to : " + seek);

                file.seek(seek);
                file.write((createString(this.number)).getBytes());
            } catch (IOException ioex) {
                ioex.printStackTrace();
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多