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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:14:01+00:00 2026-06-18T23:14:01+00:00

I have written a multi-threaded and synchronized incrementing function but it doesn’t show a

  • 0

I have written a multi-threaded and synchronized incrementing function but it doesn’t show a consistent output :-

$ java Main
count: 999883

$ java Main
count: 1000000

$ java Main
count: 999826

$ java Main
count: 1000000

$ java Main
count: 1000000

I have a synchronized counter :-

public class Counter {
    public int count;
    synchronized void inc() {
        count = count+1;
    }
    int getCount() {
        return count;
    }
}

A thread class that is initialized with a counter object and increments it 1000 times :-

public class CountPrimesRunnable implements Runnable {
    private Counter c;

    public CountPrimesRunnable(Counter c) {
        this.c = c;
    }

    public void run() {
        for (int i = 0; i < 1000; i++)
            c.inc();
    }
}

And the Main class that creates 1000 threads at a time :-

public class Main {
    public static void main(String[] args) {
        int numberOfThreads = 1000;
        Thread[] worker = new Thread[numberOfThreads];
        Counter c = new Counter();
        for (int i = 0; i < numberOfThreads; i++)
            worker[i] = new Thread(new CountPrimesRunnable(c));

        for (int i = 0; i < numberOfThreads; i++)
            worker[i].start();

        System.out.println("count: " + c.count);
    }
}

What is it that I am missing ?

  • 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-06-18T23:14:03+00:00Added an answer on June 18, 2026 at 11:14 pm

    but it doesn’t show a consistent output :-

    That’s because, it’s not sure from your code that main thread will always finish after all the other threads are done with their job. In some cases where you get result less than 1000000, are the cases, where some threads still execute after the main thread has finished.

    You can invoke Thread#join() method on each of the newly created thread to make sure that the main method waits for all those threads to die, before continuing execution after for loop.

    So, you will have to add another for loop to invoke join on each of the threads started, and also you can avoid using the 2nd for loop, by merging it with the first one:

    for (int i = 0; i < numberOfThreads; i++) {
        worker[i] = new Thread(new CountPrimesRunnable(c));
        worker[i].start();
    }
    
    for (int i = 0; i < numberOfThreads; i++) {
        worker[i].join();
    }
    

    When you invoke join on a Thread A from inside a Thread B, the Thread B will continue the further execution only after Thread A dies.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a multi threaded java code, which when runs creates 8 threads
I have just written a Java multi-threaded program in Eclipse. It compiled fine and
I have written a multi-threaded Java application which reads a bunch of .jar files
This is the strangest thing! I have a multi-threaded client application written in Python.
Here's the scenario: I have a multi threaded java web application which is running
I have a rather simple multi-threaded VCL gui application written with Delphi 2007. I
This is a simple code which I have written to learn multi threaded programming
I have a multi-threaded Win32 service written in C++ (VS2010) that makes extensive use
I have a multi-threaded application written in Python in which one thread takes care
I have never written multi-threaded code before (barring a few basic backgroundworker tricks) and

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.