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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:26:46+00:00 2026-06-15T07:26:46+00:00

For some reason, my program deadlocks when I assign both condition objects to my

  • 0

For some reason, my program deadlocks when I assign both condition objects to my lock object. When I comment out one of the condition objects, it does not deadlock. Is there something I’m missing when it comes to assigning multiple condition objects to a single lock object? The entirety of my code below just in case you wish to look at it in its entirety. Thank you so much for all your help and time in advance!

Focus on my BankAccount class which contains the lock and condition objects as instance fields:

            import java.util.concurrent.locks.Condition;
            import java.util.concurrent.locks.Lock;
            import java.util.concurrent.locks.ReentrantLock;

            public class BankAccount
            {
                public static final double MAX_BALANCE = 100000;

                private double balance;
                private Lock balanceChangeLock;
                private Condition sufficientFundsCondition; // signals that funds > 0 to allow withdrawal
                private Condition lessThanMaxBalanceCondition; // signals that balance < 100000 to allow more deposits

                /**
                 * Constructs a bank account with a zero balance
                 */
                public BankAccount()
                {
                    balance = 0;
                    balanceChangeLock = new ReentrantLock();
                    sufficientFundsCondition = balanceChangeLock.newCondition();
                    lessThanMaxBalanceCondition = balanceChangeLock.newCondition();
                }

                /**
                 * deposits money into the bank account
                 * @param amount the amount to deposit
                 * @throws InterruptedException
                 */
                public void deposit(double amount) throws InterruptedException
                {
                    balanceChangeLock.lock();
                    try
                    {
                        while(balance + amount > MAX_BALANCE)
                            lessThanMaxBalanceCondition.await();
                        System.out.print("Depositing " + amount);
                        double newBalance = balance + amount;
                        System.out.println(", new balance is " + newBalance);
                        balance = newBalance;
                        sufficientFundsCondition.signalAll();
                    }
                    finally
                    {
                        balanceChangeLock.unlock();
                    }
                }

                /**
                 * withdraws money from the bank account
                 * @param amount the amount to withdraw
                 * @throws InterruptedException
                 */
                public void withdraw(double amount) throws InterruptedException
                {
                    balanceChangeLock.lock();
                    try
                    {
                        while (balance < amount)
                            sufficientFundsCondition.await();
                        System.out.print("Withdrawing " + amount);
                        double newBalance = balance - amount;
                        System.out.println(", new balance is " + newBalance);
                        balance = newBalance;
                        lessThanMaxBalanceCondition.signalAll();
                    }
                    finally
                    {
                        balanceChangeLock.unlock();
                    }
                }

                /**
                 * gets the current balance of the bank account
                 * @return the current balance
                 */
                public double getBalance()
                {
                    return balance;
                }
            }

My Runnable objects:

            /**
             * a deposit runnable makes periodic deposits to a bank account
             */
            public class DepositRunnable implements Runnable
            {
                private static final int DELAY = 1;

                private BankAccount account;
                private double amount;
                private int count;

                /**
                 * constructs a deposit runnable
                 * @param anAccount the account into which to deposit money
                 * @param anAmount the amount to deposit in each repetition
                 * @param aCount the number of repetitions
                 */
                public DepositRunnable(BankAccount anAccount, double anAmount, int aCount)
                {
                    account = anAccount;
                    amount = anAmount;
                    count = aCount;
                }

                public void run()
                {
                    try
                    {
                        for (int i = 0; i <= count; i++)
                        {
                            account.deposit(amount);
                            Thread.sleep(DELAY);
                        }
                    }
                    catch (InterruptedException exception)
                    {
                    }
                }
            }

..

            /**
             * a withdraw runnable makes periodic withdrawals from a bank account
             */
            public class WithdrawRunnable implements Runnable
            {
                private static final int DELAY = 1;

                private BankAccount account;
                private double amount;
                private int count;

                /**
                 * constructs a withdraw runnable
                 * @param anAccount the account from which to withdraw money
                 * @param anAmount the amount to deposit  in each repetition
                 * @param aCount the number of repetitions
                 */
                public WithdrawRunnable(BankAccount anAccount, double anAmount, int aCount)
                {
                    account = anAccount;
                    amount = anAmount;
                    count = aCount;
                }

                public void run()
                {
                    try
                    {
                        for (int i = 0; i <= count; i++)
                        {
                            account.withdraw(amount);
                            Thread.sleep(DELAY);
                        }
                    }
                    catch (InterruptedException exception)
                    {
                    }
                }
            }

And my main method class where I construct my Thread objects and such:

            /**
             * this program runs threads that deposit and withdraw money from the same bank account
             */
            public class BankAccountThreadRunner
            {
                public static void main(String[] args)
                {
                    BankAccount account = new BankAccount();

                    final double AMOUNT = 10000;
                    final int REPETITIONS = 10;
                    final int DEPOSIT_THREADS = 10;
                    final int WITHDRAW_THREADS = 2;

                    for (int i = 0; i < DEPOSIT_THREADS; i++)
                    {
                        DepositRunnable deposits = 
                                new DepositRunnable(account, AMOUNT, REPETITIONS);
                        Thread depositThread = new Thread(deposits);
                        depositThread.run();
                    }

                    for (int i = 0; i < WITHDRAW_THREADS; i++)
                    {
                        WithdrawRunnable withdrawals = 
                                new WithdrawRunnable(account, AMOUNT, REPETITIONS);
                        Thread withdrawThread = new Thread(withdrawals);
                        withdrawThread.run();
                    }
                }
            }
  • 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-15T07:26:48+00:00Added an answer on June 15, 2026 at 7:26 am

    You are only using one thread. At no point does your code start or create any additional threads. You create Runnable objects, but then you never launch any threads but instead call their run methods from the main thread!

    You should never call a Runnable object’s run() method (unless you actually want to run the code in the calling thread). See this tutorial for more information.

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

Sidebar

Related Questions

This simple program below does not build for some reason. It says undefined reference
For some reason this program is saying that 'switch' is not defined. What is
For some reason the following doesn't crash like my program does, but I'm pretty
As the question says, for some reason my program is not flushing the input
for some reason my server that uses socket.io is not working properly. The program
I'm very new to programming and for some reason this program is not calculating
I have a parrallelized C++ program that does brute force optimization. For some reason
For some reason when I run the following code the program prints out JDN4
I have a following program but for some reason it is throwing an error
For some reason, dialogue boxes don't close in my program, even though shown by

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.