作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在一个文本文件上写两个线程,但它只出现了 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/
我是一名优秀的程序员,十分优秀!