java – 多线程写入文本文件

希望你能帮我这个.
我需要创建一个程序,使用多个线程写入文本文件.
我需要的是显示处理器如何给一个线程或另一个线程“注意”,所以基本上,我需要所有线程同时运行,当然,同时写入.

这是我的代码.

方法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等)

最佳答案
关闭输出之前,您必须等待线程完成其工作.最简单的方法是使用join.

    thread1.start();
    thread2.start();
    thread3.start();
    thread4.start();
    thread5.start();

    thread1.join();
    thread2.join();
    thread3.join();
    thread4.join();
    thread5.join();

    out.close();

相关文章

ArrayList简介:ArrayList 的底层是数组队列,相当于动态数组。与 Java 中的数组相比,它的容量能动态增...
一、进程与线程 进程:是代码在数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位。 线程...
本文为博客园作者所写:&#160;一寸HUI,个人博客地址:https://www.cnblogs.com/zsql/ 简单的一个类...
#############java面向对象详解#############1、面向对象基本概念2、类与对象3、类和对象的定义格式4、...
一、什么是异常? 异常就是有异于常态,和正常情况不一样,有错误出错。在java中,阻止当前方法或作用域...
Collection接口 Collection接口 Collection接口 Collection是最基本的集合接口,一个Collection代表一组...