gpt4 book ai didi

java - 使用线程来运行方法

转载 作者:行者123 更新时间:2023-12-02 02:25:37 25 4
gpt4 key购买 nike

我需要使用线程来运行 SlowFileStringifier.display() 方法。我创建了一个 Proxy 类来运行 SlowFileStringifier 类中存在的 public void display(PrintWriter out) 方法。

程序读取数据文件夹中存在的文件并显示输出。

代理 display(PrintWriter out) 应该立即输出一些文本,然后它应该启动一个线程来运行 SlowFileStringifier.display() 方法。

下面是我想要的输出:

Reading data\anaisnin.txt
Reading data\cree.txt
Reading data\maewest.txt
Reading data\marktwain.txt
Reading data\rumi.txt
"Whenever you find yourself on the side of the majority, it is time to pause and reflect."
- Mark Twain
"You only live once, but if you do it right, once is enough."- Mae West
"If you are irritated by every rub, how will your mirror be polished?"
- Jalaluddin Mevlana Rumi
"There came a time when the risk to remain tight in the bud was more painful than the risk it took to blossom."
- Anais Nin

以下是我的类(class):

import java.io.PrintWriter;

public class FileStringifierProxy implements FileStringifier{

private final String fileName;

public FileStringifierProxy(String filename) {
this.fileName = filename;
}


@Override
public void display(PrintWriter out) {
System.out.println("Reading "+fileName);
SlowFileStringifier slowfilestringifier = new SlowFileStringifier(fileName);
slowfilestringifier.display(out);
}


@Override
public String stringify() {
// TODO Auto-generated method stub
return null;
}



}

import java.io.*;
import java.util.ArrayList;

/**
* Simple file reader that turns a file into a string
*/
public class SlowFileStringifier implements FileStringifier {

/**
* Create a file stringifier with a file name
*
* @param filename
* file name of file
*/
public SlowFileStringifier(String filename) {
this.fileName = filename;
}

/**
* Writes out the file contents to the print writer
*
* @param out
* the output print writer
*/
public void display(PrintWriter out) {
out.println(this.stringify());
out.flush();
}

/**
* Returns a string with all the files contents
*/
public String stringify() {
FileReader f;
try {
f = new FileReader(this.fileName);
BufferedReader bf = new BufferedReader(f);

ArrayList<String> lines = new ArrayList<String>();
String line = null;
while ((line = bf.readLine()) != null) {
Thread.sleep(2000);
lines.add(line);
}
bf.close();
return String.join("\n", lines);

} catch (FileNotFoundException e) {
e.printStackTrace();
System.err.println("File " + this.fileName + " was not found.");
} catch (IOException e) {
e.printStackTrace();
System.err.println("Problem reading file " + this.fileName + ".");
} catch (InterruptedException e) {
e.printStackTrace();
System.err.println("File reading interrupted: " + this.fileName);
}
return "";
}

private final String fileName;
}

public class ShowFileContents {

/**
* Accepts a file directory and then prints out the contents of all the
* files in that directory
*
* @param args
* single arg with directory path
*/
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage: java ShowFileContents <directory>");
}

String dirname = args[0];

File dir = new File(dirname);
if (!dir.exists()) {
System.err.println(dirname + " does not exist");
return;
}
if (!dir.isDirectory()) {
System.err.println(dirname + " is not a directory");
return;
}

PrintWriter outWriter = new PrintWriter(System.out);
for (File file : dir.listFiles()) {
if (file.isDirectory())
continue;
FileStringifier fd = new FileStringifierProxy(file.getPath());
fd.display(outWriter);
}
}

}

下面是我现在得到的输出:

Reading data\anaisnin.txt
"There came a time when the risk to remain tight in the bud was more painful than the risk it took to blossom."
- Anais Nin
Reading data\cree.txt
"When all the trees have been cut down, when all the animals have been hunted, when all the waters are polluted, when all the air is unsafe to breathe, only then will you discover you cannot eat money."
- Cree Prophecy
Reading data\maewest.txt
"You only live once, but if you do it right, once is enough."
- Mae West
Reading data\marktwain.txt
"Whenever you find yourself on the side of the majority, it is time to pause and reflect."
- Mark Twain
Reading data\rumi.txt
"If you are irritated by every rub, how will your mirror be polished?"
- Jalaluddin Mevlana Rumi

最佳答案

您可以使用 CountDownLatch(由 @Xingbin Sun 建议)和 CyclicBarrier。两者都提供了线程同步的方式。在您的情况下,您希望线程等待完成写入名称,然后继续处理内容。

为了解决您的具体问题,您可能需要使用以下代码。

你的主要方法:

public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage: java ShowFileContents <directory>");
}

String dirname = args[0];

File dir = new File(dirname);
if (!dir.exists()) {
System.err.println(dirname + " does not exist");
return;
}
if (!dir.isDirectory()) {
System.err.println(dirname + " is not a directory");
return;
}

File[] files = dir.listFiles();
int noOfFilesToProcess = 0;
for (final File file : files) {
if (!file.isDirectory()) {
noOfFilesToProcess++;
}
}
final CountDownLatch startSignal = new CountDownLatch(1);
final CountDownLatch doneSignal = new CountDownLatch(noOfFilesToProcess);
final PrintWriter outWriter = new PrintWriter(System.out);
for (final File file : files) {
if (file.isDirectory())
continue;
Thread thread = new Thread() {
public void run() {
FileStringifier fd = new FileStringifierProxy(
file.getPath(), startSignal, doneSignal);
fd.display(outWriter);
}
};
thread.start();
}
// Do something here
startSignal.countDown(); // let all threads proceed
// Do something here while other threads are processing
try {
doneSignal.await(); // wait for all to finish
} catch (InterruptedException e) {
e.printStackTrace();
}
// Do something after all thread finishes
}

FileStringifierProxy 的实现:

class FileStringifierProxy implements FileStringifier {

private final String fileName;
private final CountDownLatch startSignal;
private final CountDownLatch doneSignal;

public FileStringifierProxy(String filename, CountDownLatch startSignal,
CountDownLatch doneSignal) {
this.fileName = filename;
this.startSignal = startSignal;
this.doneSignal = doneSignal;
}

public void display(final PrintWriter out) {
System.out.println("Reading " + fileName);
final SlowFileStringifier slowfilestringifier = new
SlowFileStringifier(
fileName);
try {
startSignal.await(); // Wait for start signal from main thread
} catch (InterruptedException e) {
e.printStackTrace();
}
slowfilestringifier.display(out); // Display file content
doneSignal.countDown(); // Send done signal to main thread
}

public String stringify() {
// TODO Auto-generated method stub
return null;
}
}

关于java - 使用线程来运行方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47825178/

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