简体   繁体   English

如何从多个线程写入文件

[英]How to write in file from multiple threads

I have problem. 我有问题。 I need to create 9 files, each called from thread name. 我需要创建9个文件,每个文件都从线程名调用。 Each file will be called 1.txt, 2.txt, 3.txt etc. Each file will be filled with a symbol that corresponds to the name of the file (1.txt file is "1"). 每个文件将被称为1.txt,2.txt,3.txt等。每个文件将填充一个与文件名相对应的符号(1.txt文件为“ 1”)。 Each file should be 100 lines, each line length is 100 characters. 每个文件应为100行,每行长度为100个字符。 This work must perform threads of execution and I\\O. 这项工作必须执行执行和I \\ O线程。 I need to read the contents of these files in the resulting file super.txt, when using several threads. 使用多个线程时,我需要在生成的文件super.txt中读取这些文件的内容。

My code: 我的代码:

public class CustomThread extends Thread {

    Thread t;
    String threadName;

    CustomThread(String threadName) {
        this.threadName = threadName;
    }

    @Override
    public void run() {
        if (t == null) {
            t = new Thread(this);
        }
        add(threadName);
    }

    public void add(String threadName) {
        File f = new File(threadName + ".txt");

        if (!f.exists()) {
            try {
                f.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("File does not exists!");
            }
        }

        FileWriter fw = null;
        try {
            fw = new FileWriter(f);
            for (int i = 0; i < 100; i++) {
                for (int j = 0; j < 100; j++) {
                    fw.write(threadName);
                }
                fw.write('\n');
            }

        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("File does not exists!");
        }
    }
}

My main: 我的主要:

public class Main {
    public static void main(String[] args) {

        CustomThread T1 = new CustomThread("1");
        T1.start();

        CustomThread T2 = new CustomThread("2");
        T2.start();

    }
}

First question. 第一个问题。 I need, to make threads in cycle. 我需要使线程处于循环状态。 Look on my main: I create 看我的主:我创造

CustomThread T1 = new CustomThread("1");
T1.start();

But, i want to create 9 files in cycle. 但是,我想循环创建9个文件。 How to do this ? 这个怎么做 ?

Second question. 第二个问题。 I need to write in every my file from multiple threads. 我需要从多个线程写入每个文件。

Third question. 第三个问题。 How to write from multiple threads in result file five contents of thats files ? 如何从多个线程写入结果文件thats文件的五个内容?

i want to create 9 files in cycle. 我想循环创建9个文件。 How to do this ? 这个怎么做 ?

Use a loop 使用循环

for (int i = 1; i <= 9; i++) {
   new CustomThread("" + i).start();
}

I need to write in every my file from multiple threads. 我需要从多个线程写入每个文件。

How do you do this? 你怎么做到这一点? Open the files before you start the threads and lock them when ever you use them. 在启动线程之前先打开文件,并在使用它们时将其锁定。

How to write from multiple threads in result file five contents of thats files ? 如何从多个线程写入结果文件thats文件的五个内容?

Can you rephrase that question? 你能改一下这个问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM