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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:40:51+00:00 2026-05-25T14:40:51+00:00

I have a deposit function which is called by multiple clients at same time.

  • 0

I have a deposit function which is called by multiple clients at same time. I want the deposit function synchronized when same client (say client with id=someUniqueNo) calls it again but not synchronized when other client else call it ? How can I achieve this.
Using Spring framework in the project , Its cool even if any solution with the framework provides for such pattern.

  • 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-25T14:40:51+00:00Added an answer on May 25, 2026 at 2:40 pm
    public void deposit(String clientId){
       String id = clientId;
       while(id.equals("someClientID")){
             synchronized(lockObject){
                 // Do your stuff here in a sync'd way
              }
       }
       else
       {
         // Do your stuff here in a NON synch'd way 
       }
    
     }
    

    Second attempt : ( not tested !)

    public void deposit(String clientId){
        String id = clientId;
       // callers is a ConcurrenthashMap<String,String>
         while(callers.get(id) != null){  
         synchronized(callers.get(id)){  // client already in call, so wait
            wait(); 
         }
       }       
          callers.put(id,id);  // client is ready to run a new call, so setup 
          // Do your stuff here in a NON synch'd way 
     synchronized(callers.get(id)){  // client done with a call , 
                          //so notify others who are calling
        callers.put(id,null);
        notifyAll();
           }
    }  
    

    Third attempt ( tested and working correctly )

        public class SyncTest {
         static int[] balances = new int[]{ 1,10,100,1000,10000};
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SyncTest test = new SyncTest();
        Client c1 = new Client("0", test,1);
        Client c2 = new Client("1", test,10);
        Client c3 = new Client("2", test,100);
        Client c4 = new Client("3", test,1000);
        Client c5 = new Client("4", test,10000);
    
        Thread[] threads = new Thread[5];
        threads[0] = new Thread(c1);
        threads[1] = new Thread(c2);
        threads[2] = new Thread(c3);
        threads[3] = new Thread(c4);
        threads[4] = new Thread(c5);
    
        for(int i=0;i<5;i++){
            threads[i].start();
        }
        for(int i=0;i<5;i++){
            try{
                threads[i].join();
            }
            catch(InterruptedException ex){
            }
        }
    
    
        System.out.println("Final balances are ");
        for(int i=0;i<5;i++){
            System.out.print(balances[i] + " , " );
        }
    }
    
    ConcurrentHashMap<String,String> callers = new ConcurrentHashMap<String, String>();
    
    public void deposit(Client c) throws InterruptedException{
        String id = c.id;
        int amount = c.amount;
        if(id == null) return ;
    
       // callers is a ConcurrenthashMap<String,String>
        System.out.println("Client " + id + " in deposit. Checking if already running..");
        while( callers.get(id) != null && !(callers.get(id).equals("X"))) {
           synchronized(id){
            System.out.println("Client " + id + " in deposit. Found already running,going to wait..");
            id.wait(); 
         }
       }
        if(callers == null){
            throw new NullPointerException("Callers is null!!");
        }
    
        System.out.println("Client " + id + " in deposit. Found not running already,  Now running..");
        callers.put(id,id);
        int index = Integer.parseInt(id);
        balances[index] += amount;
        Thread.sleep(2000);
        synchronized(id){
            String old = callers.put(id,"X");
            id.notifyAll();
           System.out.println("Client " + id + " in deposit. Finished running ,  Notifying..");
        }
    }
    
    static class Client implements Runnable {
        String id ;
        SyncTest test;
        int amount;
        Client c = this;
    
        Client(String id , SyncTest t, int am){
            this.id = id;
            test = t;
            amount = am;
        }
    
        public void run(){
            process();
        }
    
        void process(){
            System.out.println("Client " + id + " processing ...");
            try{
                Thread.sleep(1000);
                Thread t1 = new Thread(
                    new Runnable(){
                        public void run(){
                            try{
                                test.deposit(c);
                            }
                            catch(InterruptedException ex){
                            }
                        }
                    }
                );
                Thread t2 = new Thread(
                        new Runnable(){
                            public void run(){
                                try{
                                    test.deposit(c);
                                }
                                catch(InterruptedException ex){
                                }
                            }
                        }
                    );
    
                Thread t3 = new Thread(
                        new Runnable(){
                            public void run(){
                                try{
                                    test.deposit(c);
                                }
                                catch(InterruptedException ex){
                                }
                            }
                        }
                    );
    
                Thread t4 = new Thread(
                        new Runnable(){
                            public void run(){
                                try{
                                    test.deposit(c);
                                }
                                catch(InterruptedException ex){
                                }
                            }
                        }
                    );
    
            /*  Thread t5 = new Thread(
                        new Runnable(){
                            public void run(){
                                try{
                                    test.deposit(c);
                                }
                                catch(InterruptedException ex){
                                }
                            }
                        }
                    );
    
                Thread t6 = new Thread(
                        new Runnable(){
                            public void run(){
                                try{
                                    test.deposit(c);
                                }
                                catch(InterruptedException ex){
                                }
                            }
                        }
                    );
                Thread t7 = new Thread(
                        new Runnable(){
                            public void run(){
                                try{
                                    test.deposit(c);
                                }
                                catch(InterruptedException ex){
                                }
                            }
                        }
                    );
                Thread t8 = new Thread(
                        new Runnable(){
                            public void run(){
                                try{
                                    test.deposit(c);
                                }
                                catch(InterruptedException ex){
                                }
                            }
                        }
                    );
            */  
                t1.start();t2.start();t3.start();t4.start();//t5.start();t6.start();t7.start();t8.start();
                t1.join();t2.join();t3.join();t4.join();//t5.join();t6.join();t7.join();t8.join();
    
            }
            catch(InterruptedException ex){
                System.out.println(" Exception " + ex.getMessage());
            }
            System.out.println("Client " + id + " done #####");
        }
    }
    

    }

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

Sidebar

Related Questions

Now I'm sure this is super easy, I have a function called remove_deposit which
this is the function I have, in my login controller, which stores data into
I want to have generalised email templates. Currently I have multiple email templates with
Say I have the following data in my table; tran_date withdraw deposit 25/11/2010 0
Let's say I have a table named transaction with transaction_date, deposit, withdrawal fields. There
Have a scenario with a client's new crm where they have suppliers and clients
Say I have this column returned in a command for Crystal: deposit_no 123 130
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have the following simple function: private void EnableDisable941ScheduleBButton() { if (this._uosDepositorFrequency.Value != null)
I have a function that gets a value from the database and returns 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.