Note that there are some explanatory texts on larger screens.

plurals
  1. POWill the use of a Bounded Buffer (Producer/Consumer) avoid the pain of having sync methods/deadlocks?
    text
    copied!<p>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. </p> <p>I have made buffer's put() and get() methods to have conditional synchronization. </p> <p>ex: </p> <pre><code>// put method while(total_buffer_size == current_total_requests) { System.out.println("Buffer is full"); wait(); } </code></pre> <p>So my question is, <strong>do we have to synchronize methods</strong> like get-balance or transfer-funds to avoid corruption of data? I believe it is not necessary since the Buffer Reader takes each request <strong>one-by-one</strong> and relevant action. <strong>Have I avoided any deadlock situations</strong> through this? What do you think? Thanks</p> <p>EDIT2: </p> <pre><code>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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload