希望你能帮我这个.我需要创建一个程序,使用多个线程写入文本文件.我需要的是显示处理器如何给一个线程或另一个线程"注意",所以基本上,我需要所有线程同时运行,当然,同时写入.
这是我的代码.
方法1:使用"for"创建和启动线程.
public class ThreadGenerator {
public static void main(String[] args) {
File textFile = new File("c:\\threadLog.txt");
try {
PrintWriter out = new PrintWriter(new FileWriter(textFile));
for (int index = 0; index < 5; index++) {
ThreadCustom thread = new ThreadCustom("ID" + index, out);
thread.start();
}
out.close();
} catch (IOException ex) {
Logger.getLogger(ThreadGenerator.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Run Code Online (Sandbox Code Playgroud)
方法2:手动创建和启动每个线程
public class ThreadGenerator {
public static void main(String[] args) {
File textFile = new File("c:\\threadLog.txt");
try {
PrintWriter out = new …Run Code Online (Sandbox Code Playgroud)