Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7189547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:19:32+00:00 2026-05-28T19:19:32+00:00

Hope you can help me with this one. I need to make a program

  • 0

Hope you can help me with this one.
I need to make a program that, using multiple threads, writes into a text file.
What i need is to show how the processor gives “attention” to one thread or another, so basically, i need all the threads running at the same time and, of course, writing at the same time.

Here’s my code.

Method 1: Using a “for” to create and start threads.

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);
        }

    }
}

Method 2: Creating and starting each thread manually

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);
        }

    }
}

This is my ThreadCustom class

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");
    }
}

So, as you can see, i create a PrinterWriter, and give it as a parameter to create a ThreadCustom object, so all the threads use the same PrinterWriter object (All objects in java are passed as reference, right?)

What im expecting to get? Something like this

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

or something like that.

Hope you can help me!

Thanks in advance community!!

PS: Using the .start() does create the .txt, but doesnt write anything on it, but, if i use .run() instead of .start(), it does write into the .txt, but sequentially (ID0, ID1, ID3, and so on)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:19:33+00:00Added an answer on May 28, 2026 at 7:19 pm

    You have to wait for your threads to complete their work before closing the output. Simplest way to do this is to use join.

        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        thread5.start();
    
        thread1.join();
        thread2.join();
        thread3.join();
        thread4.join();
        thread5.join();
    
        out.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hope you can help me with this one… I saw some crazy behavior so
I hope someone can help me with this, as I'm unable to find the
I hope you can help. This has been confounding me for hours. I have
I hope there's a SharePoint expert here on SO who can help with this.
I hope some of you guys can help me with this problem.... I have
I hope you can help me. I have a web service that needs to
my SQL skills aren't strong enough to figure this out, hope someone can help.
I hope this is the right place to post this and somebody can help.
hope someone can help. I have two tables: Users -UserID -UserName UsersType -UserTypeID -UserID
I hope somebody can help me. I've been trying to write a custom html

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.