希望你能帮我解决这个问题。 我需要创建一个程序,使用多个线程写入文本文件。 我需要的是显示处理器如何给一个线程或另一个线程“注意”,所以基本上,我需要所有线程同时运行,当然,同时写入。
这是我的代码。
方法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);
}
}
}
方法2:手动创建和启动每个线程
public class ThreadGenerator {
public static void main(String[] args) {
File textFile = new File("c:\\threadLog.txt");
try {
PrintWriter out = new PrintWriter(new FileWriter(textFile));
ThreadCustom thread1 = new ThreadCustom("ID1", out);
ThreadCustom thread2 = new ThreadCustom("ID2", out);
ThreadCustom thread3 = new ThreadCustom("ID3", out);
ThreadCustom thread4 = new ThreadCustom("ID4", out);
ThreadCustom thread5 = new ThreadCustom("ID5", out);
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
out.close();
} catch (IOException ex) {
Logger.getLogger(ThreadGenerator.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
这是我的ThreadCustom类
public class ThreadCustom extends Thread {
private String threadId;
private PrintWriter out;
public ThreadCustom(String threadId, PrintWriter out){
this.threadId = threadId;
this.out = out;
}
@Override
public void run(){
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
Calendar cal = Calendar.getInstance();
this.out.println("Thread ID: "+this.threadId+" Enter Time: "+cal.getTime()+"\n");
for(int index = 0; index < 10000; index++){
this.out.println("Thread ID: "+this.threadId+" Current Time: "+cal.getTime()+"\n");
}
this.out.println("Thread ID: "+this.threadId+" Exit Time: "+cal.getTime()+"\n");
}
}
因此,正如您所看到的,我创建了一个PrinterWriter,并将其作为参数来创建ThreadCustom对象,因此所有线程都使用相同的PrinterWriter对象(java中的所有对象都作为引用传递,对吧?) / p>
我期待得到什么?像这样的东西
Thread ID: ID0 Enter Time: Fri Jan 27 00:38:54 CLST 2012
Thread ID: ID0 Current Time: Fri Jan 27 00:38:54 CLST 2012
Thread ID: ID1 Enter Time: Fri Jan 27 00:38:54 CLST 2012
Thread ID: ID0 Current Time: Fri Jan 27 00:38:54 CLST 2012
Thread ID: ID0 Current Time: Fri Jan 27 00:38:54 CLST 2012
Thread ID: ID1 Current Time: Fri Jan 27 00:38:54 CLST 2012
Thread ID: ID0 Current Time: Fri Jan 27 00:38:54 CLST 2012
Thread ID: ID1 Current Time: Fri Jan 27 00:38:54 CLST 2012
Thread ID: ID0 Current Time: Fri Jan 27 00:38:54 CLST 2012
Thread ID: ID0 Current Time: Fri Jan 27 00:38:54 CLST 2012
Thread ID: ID Exit Time: Fri Jan 27 00:38:54 CLST 2012
Thread ID: ID0 Current Time: Fri Jan 27 00:38:54 CLST 2012
Thread ID: ID0 Current Time: Fri Jan 27 00:38:54 CLST 2012
Thread ID: ID1 Current Time: Fri Jan 27 00:38:54 CLST 2012
Thread ID: ID0 Current Time: Fri Jan 27 00:38:54 CLST 2012
Thread ID: ID0 Current Time: Fri Jan 27 00:38:54 CLST 2012
Thread ID: ID1 Exit Time: Fri Jan 27 00:38:54 CLST 2012
或类似的东西。
希望你能帮助我!
先谢谢社区!!
PS:使用.start()会创建.txt,但不会在其上写任何东西,但是,如果我使用.run()而不是.start(),它会写入.txt,但是顺序(ID0,ID1,ID3等)
答案 0 :(得分:4)
在关闭输出之前,您必须等待线程完成其工作。最简单的方法是使用join
。
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
thread1.join();
thread2.join();
thread3.join();
thread4.join();
thread5.join();
out.close();