我想在一个文本文件上写两个线程,但它只出现在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();
}
}
}
答案 0 :(得分:1)
试试这个。这里我在print writer中使用append to file。我没有对您的代码做过很多更改。
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();
}
}
}