gpt4 book ai didi

java - 如何将多线程写入单个文本文件?

转载 作者:行者123 更新时间:2023-11-30 10:18:29 25 4
gpt4 key购买 nike

我想在一个文本文件上写两个线程,但它只出现了 PrintNum 部分而 PrintChar 不在文本文件中?如何将两个线程写入一个文件?有什么方法可以将两个线程组合成一个类,以便它可以输出到一个文件中吗?

这是我的代码:

import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class Multithread {

public static void main(String[] args) {
Runnable printA = new PrintChar('a', 500);
Runnable print100 = new PrintNum(500);

Thread thread1 = new Thread(printA);
Thread thread2 = new Thread(print100);

thread1.start();
thread2.start();
}
}

class PrintChar implements Runnable {
private char charToPrint;
private int times;

public PrintChar(char c, int t) {
charToPrint = c;
times = t;
}

@Override
public void run() {
String fileName = "out.txt";
try {
PrintWriter outputStream = new PrintWriter(fileName);
for (int i = 0; i < times; i++) {
outputStream.print(charToPrint);
}
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

class PrintNum implements Runnable {
private int lastNum;

public PrintNum(int n) {
lastNum = n;
}

@Override
public void run() {
String fileName = "out.txt";
try {
PrintWriter outputStream = new PrintWriter(fileName);
for (int i = 1; i <= lastNum; i++) {
outputStream.print(" " + i);
}
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

最佳答案

试试这个。在这里,我在打印编写器中使用附加到文件。我没有对您的代码做太多更改。

import java.io.*;

public class Multithread {

public static void main(String[] args) throws FileNotFoundException {
String fileName = "out.txt";
new PrintWriter(fileName).close();//to clear the text of the file
Runnable printA = new PrintChar('a', 500);
Runnable print100 = new PrintNum(500);

Thread thread1 = new Thread(printA);
Thread thread2 = new Thread(print100);

thread1.start();
thread2.start();
}
}

class PrintChar implements Runnable {
private char charToPrint;
private int times;

public PrintChar(char c, int t) {
charToPrint = c;
times = t;
}

@Override
public void run() {
String fileName = "out.txt";
try {
synchronized (Multithread.class) {//to writ by one thread
FileWriter fw = new FileWriter(fileName, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter outputStream = new PrintWriter(bw);
for (int i = 0; i < times; i++) {
outputStream.print(charToPrint);
}
outputStream.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}

class PrintNum implements Runnable {
private int lastNum;

public PrintNum(int n) {
lastNum = n;
}

@Override
public void run() {
String fileName = "out.txt";
try {
synchronized (Multithread.class) {//to write by one thread
FileWriter fw = new FileWriter(fileName, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter outputStream = new PrintWriter(bw);
for (int i = 1; i <= lastNum; i++) {
outputStream.print(" " + i);
}
outputStream.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}

关于java - 如何将多线程写入单个文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49227091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com