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 6820505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:25:58+00:00 2026-05-26T21:25:58+00:00

I have multiple threads printing data. All the data does print; however, they are

  • 0

I have multiple threads printing data. All the data does print; however, they are not in order. Meaning Thread 2 starts printing while Thread 1 is not done yet, which causes inconsistency. How do I guarantee consistency ? Meaning Thread 2 can print once Thread 1 is done.

  • 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-26T21:25:58+00:00Added an answer on May 26, 2026 at 9:25 pm

    From your comments, you are looking to achieve 1 1 1 1 2 2 2 2 so what you’d need to do is actually wait for Thread1 to complete, then start Thread2, like this:

    Thread1 thread1 = new Thread1();
    Thread2 thread2 = new Thread2();
    
    thread1.start();
    
    thread1.join(); // This waits for Thread1 to finish before starting Thread2
    thread2.start();
    

    however, if you want to have both threads start, but have Thread2 wait part way through its processing, while waiting for Thread1 to complete – this can be done with a few techniques – one in particular comes to mind – using Semaphore.

    Example:

    Ok, firstly define your thread constructors to accept a Semaphore, thus:

    // Thread2 would be identical wrt the semaphore
    public Thread1(Semaphore semaphore) throws InterruptedException
    {
      this.semaphore = semaphore; // store semaphore locally
      semaphore.acquire(2);
    }
    

    Thread1’s run method would look like this:

    public void run()
    {
      // .... processing...
    
      // ... print stuff
      System.out.println("1 1 1 1");
      semaphore.release(); // release 1 permit, allowing thread2 to continue
    
      // ... more processing
    
      semaphore.release(); // release last permit.
    }
    

    Thread2’s run method would look something akin to this:

    public void run()
    {
      int permits = 2;
      try
      {
        // .... processing...
    
        semaphore.acquire(1); // acquire one more permit (will block until Thread1 releases one)
        permits++;
    
        // ... print stuff
        System.out.println("2 2 2 2");
    
        // ... more processing      
      }
      catch (InterruptedException ie)
      {
        // interrupted
      }
    
      semaphore.release(permits); // release permits.
    }
    

    Now, setting up the threads would be done like this:

    try
    {
      Semaphore semaphore = new Semaphore(4); // 4 permit semaphore
    
      Thread1 thread1 = new Thread1(semaphore);
      Thread2 thread2 = new Thread2(semaphore);
    
      thread1.start();
      thread2.start();
    
      semaphore.acquire(4);
    }
    catch (InterruptedException ie)
    {
      // interrupt logic...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple threads (C# application running on IIS) running that all need to
I have multiple threads who all need to write to the same Dictionary. I
I have multiple threads that share use of a semaphore. Thread A holds the
I have multiple threads each one with its own private concurrent queue and all
I have an object (Client * client) which starts multiple threads to handle various
I have multiple threads adding, modifying and looking up data in GHashTable. Is it
I have multiple threads which are serializing my 'Data' objects to files. The filename
I have multiple threads, executing similar queries. They shouldn't be executed the same time.
I have a application which uses .net Thread-pool to have multiple threads.It uses log4net
Can/Does WPF have multiple GUI threads? Or does it always only have one GUI

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.