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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:27:32+00:00 2026-05-26T05:27:32+00:00

I’m coding a simple Bank simulator where users would login from different locations at

  • 0

I’m coding a simple Bank simulator where users would login from different locations at once, using sockets. In the Bank server I keep a bounded buffer to store every incoming request, ex: transfer funds, get account balance etc and there’s a Background Thread running at Server end (Buffer Reader) to pull out each request from this Request Queue (assume it works as a Thread Scheduler in OS), in FCFS basis.

I have made buffer’s put() and get() methods to have conditional synchronization.

ex:

// put method
while(total_buffer_size == current_total_requests) {

 System.out.println("Buffer is full");
 wait();

}

So my question is, do we have to synchronize methods like get-balance or transfer-funds to avoid corruption of data? I believe it is not necessary since the Buffer Reader takes each request one-by-one and relevant action. Have I avoided any deadlock situations through this? What do you think? Thanks

EDIT2:

public synchronized boolean put(Messenger msg, Thread t, Socket s) throws InterruptedException {
        while(total_buffer_size  == current_total_requests) {

            System.out.println("Buffer is full");
            wait();

        }
        current_total_requests++;

        requests[cur_req_in] = new Request(msg, s); // insert into Queue

        cur_req_in = (cur_req_in + 1) % total_buffer_size ;

        notifyAll();

        return true;
    }

    // take each incoming message in queue. FIFO rule followed
    public synchronized Request get() throws InterruptedException {

            while(current_total_requests==0) wait();
            Request out = requests[cur_req_out];
            requests[cur_req_out] = null;

            cur_req_out = (cur_req_out + 1) % total_buffer_size ;
            current_total_requests--;
            notifyAll(); //wake all waiting threads to continue put()
            return out;

    }
  • 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-26T05:27:32+00:00Added an answer on May 26, 2026 at 5:27 am

    If there is only one consumer (i.e. one thread that consumes the requests from the “buffer”) , then you don’t need to use any synchronization on the methods relating to the bank account. However, I don’t believe that your current implementation of a “bounded buffer” is valid. To be more specific:

    while(total_buffer_size == current_total_requests) {
    
     System.out.println("Buffer is full");
     wait();
    
    }
    

    There is absolutely no guarantee how many threads will get past the while loop, perform a context switch just before current_total_requests is incremented and queue more requests than what’s allowed the buffer size. Unless your put method is synchronized, this approach will be extremely unreliable and prone to race conditions.

    If you want a bounded buffer, then just use one of Java’s already existing “bounded buffers” or more specifically: the BlockingQueue. The BlockingQueue blocks on put(...):

    Inserts the specified element into this queue, waiting if necessary for space to become available.

    It also blocks on take() if there is no data in the queue. I don’t know if you can use one of the items in the concurrency library, but if you can’t then you have to fix your BoundedBuffer.

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

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.