Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: From single-threaded to multi-threaded Java code

  1. #1
    Junior Member
    Join Date
    Apr 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question From single-threaded to multi-threaded Java code

    Hello.

    I would like to know how to turn the following Java code into a multi-threaded one where a thousand threads are created to each write one integer into the output file:

    public void writeIntegersToFile () {
        File outputFile = new File("FirstThousandIntegers.txt");
        try {
            FileWriter out = new FileWriter(outputFile);
            PrintWriter writer = new PrintWriter(out);
            for (int p=0; p<1000; p++)
                    writer.println(p);
            out.close();
        }
        catch (IOException e) {
            System.out.println("Couldn't open " + outputFile.getName());
        }        
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: From single-threaded to multi-threaded Java code

    What is the purpose of making that change?
    There will be a contention problem when more than one thread tries to write to the same file at the same time. For them to not get in each others way, each thread will have to wait its turn. The end result will take much longer than doing it on one thread.

    One way to use 1000 threads would be to create the threads one at a time that write its number to the file and exits before the next thread is created.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. When you have a multi-threaded Server and Client program....
    By ineedahero in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 18th, 2013, 09:14 PM
  2. [SOLVED] My chat program's multi threaded server keeps on throwing exceptions when calling nextLine();
    By Chromejuice in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 2nd, 2012, 10:52 PM
  3. Replies: 3
    Last Post: July 24th, 2011, 06:13 AM
  4. Multi-Threaded Chat Server
    By TopdeK in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 7th, 2011, 10:12 AM
  5. Replies: 1
    Last Post: October 19th, 2009, 11:53 PM

Tags for this Thread