希望你能帮我这个.
我需要创建一个程序,使用多个线程写入文本文件.
我需要的是显示处理器如何给一个线程或另一个线程“注意”,所以基本上,我需要所有线程同时运行,当然,同时写入.
这是我的代码.
方法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,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中的所有对象都作为引用传递,对吧?)
我期待得到什么?像这样的东西
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等)
最佳答案