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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:39:09+00:00 2026-06-15T23:39:09+00:00

I am trying to figure out what is wrong with this thread program in

  • 0

I am trying to figure out what is wrong with this thread program in java. Can anyone shed some light? Here is the code:

public class Z {
    private Account account = new Account();
    private Thread[] thread = new Thread[100];

    public static void main(String[] args) {
        Z test = new Z();
        System.out.println("What is balance ? " + test.account.getBalance());
    }

    public Z() {
        ThreadGroup g = new ThreadGroup("group");
        boolean done = false;

        // Create and launch 100 threads
        for (int i = 0; i < 100; i++) {
            thread[i] = new Thread(g, new AddAPennyThread(), "t" + i);
            thread[i].start();
            System.out.println("depositor: " + thread[i].getName());
        }
        // Check if all the threads are finished
        while (!done)
            if (g.activeCount() == 0)
                done = true;
    }

    // A thread for adding a penny to the account
    class AddAPennyThread extends Thread {
        public void run() {
            account.deposit(1);
        }
    }

    // An inner class for account
    class Account {
        private int balance = 0;

        public int getBalance() {
            return balance;
        }

        public void deposit(int amount) {
            int newBalance = balance + amount;
            balance = newBalance;
        }
    }
}

It compiles and runs fine. It was a test question I missed and wnated to know what is actually wrong with it. Thanks!

  • 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-15T23:39:10+00:00Added an answer on June 15, 2026 at 11:39 pm

    There is not a single bit dedicated to synchronization of 100 threads all working on exactly one (1!!!) piece of data.

    Anything can happen. “Anything” includes that the code as it is works most of the time due some “coincidences”:

    • The task at hand is quite small. (only an add)
    • The tasks are created and immediately started.
    • There is a small delay between two “create+start” pair: the System.out.println.

    This adds up to: This might work in most test-runs. But it is an incorrect and non-deterministic program.

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

Sidebar

Related Questions

I can't figure out where I'm going wrong here. Trying to validate an email
i'm trying to figure out what is wrong in this code. it either doesn't
I am trying to figure out what is wrong with this code, I need
I've been trying to figure out what is wrong with my code here. The
I'm trying to figure out what's wrong with my mysqli code below. The data
Trying to figure out how I can do this properly. The print_r looks like
I spend a lot of time trying to figure out what's wrong with this
I have some problems trying to figure out what is wrong with my object
Been trying to figure out what's wrong with this one for a while. I'm
So I'm trying to figure out what I'm doing wrong with this logic. It

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.