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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:23:00+00:00 2026-05-11T21:23:00+00:00

programmer mates. I was testing java threading capabilities with a very simple code (or

  • 0

programmer mates. I was testing java threading capabilities with a very simple code (or at least it seemed simple). I have this class Account:

public class Account {
    protected double balance;

    public synchronized void withdraw(double value) {
        this.balance = this.balance - value;
    }

    public synchronized void deposit(double value) {
        this.balance = this.balance + value;
    }

    public synchronized double getBalance() {
        return this.balance;
    }
}

And I have two threads: Depositer, that deposits $10 a thousand times:

public class Depositer extends Thread {
    protected Account account;

    public Depositer(Account a) {
        account = a;
    }

    @Override
    public void run() {
        for(int i = 0; i < 1000; i++) {
            this.account.deposit(10);
        }
    }
}

And Withdrawer, that withdraws $10 a thousand times:

public class Withdrawer extends Thread {
    protected Account account;

    public Withdrawer(Account a) {
        account = a;
    }

    @Override
    public void run() {
        for(int i = 0; i < 1000; i++) {
            this.account.withdraw(10);
        }
    }
}

This arrangement is executed by:

public class Main {
    public static void main(String[] args) {
        Account account = new Account();
        Thread c1 = new Depositer(account);
        Thread c2 = new Withdrawer(account);

        c2.start();
        c1.start();

        System.out.println(account.getBalance());
    }
}

As the methods are sychronized, I just expected that the balance was always 0 at the end, but this not happens sometimes. And I sincerely cannot figure out why. Can someone see where is my fault?

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

    You do the following:

    c2.start();  // Start a thread in the background
    c1.start();  // Start a 2nd thread in the background
    
    // print out the balance while both threads are still running
    System.out.println(account.getBalance());
    

    You need to wait for these threads to complete their processing:

    c2.start();  // Start a thread in the background
    c1.start();  // Start a 2nd thread in the background
    
    try {
        c2.join();  // Wait until the c2 thread completes
        c1.join();  // Wait until the c1 thread completes
    } catch (InterruptedException e) {
        // LOG AN ERROR HERE
    }
    
    // print out the final balance
    System.out.println(account.getBalance());
    

    If you interrupt your main Thread then you’ll need to do something with an interrupted exception. Assuming that none of your code does this, you should always, at a minimum, log the Exception. NOTE: You’ll get the InterruptedException not if someone interrupts c1 or c2, but if someone interrupts your main thread, the thread that calls join(). If someone has called interrupt() on your main thread but you don’t check for it, then you’ll probably get the InterruptedException the moment you call join().

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

Sidebar

Ask A Question

Stats

  • Questions 120k
  • Answers 120k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you subclass NSManagedObject and override -didTurnIntoFault, you must send… May 12, 2026 at 12:03 am
  • Editorial Team
    Editorial Team added an answer The nxt-python and ruby-nxt projects are remote control interfaces to… May 12, 2026 at 12:03 am
  • Editorial Team
    Editorial Team added an answer For individual cmdlets, there is a parameter called -erroraction. The… May 12, 2026 at 12:03 am

Related Questions

The preamble: I have designed a strongly interfaced and fully mockable data layer class
Edit: This code is fine. I found a logic bug somewhere that doesn't exist
Background While at the Gym the other day, I was working with my combination
I've been using flash for a long time, mostly as an animation tool with

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.