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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:34:52+00:00 2026-06-08T17:34:52+00:00

Following is a rather common scenario of accessing a common resource, either in a

  • 0

Following is a rather common scenario of accessing a common resource, either in a sequential (single-threaded) or a concurrent (multi-threaded) way, for which the fastest technique is needed.

More specifically (see sample source code below), a Manager class creates some instances of a Runnable (or Callable) class (Handler) with a common resource (a Store object). The Manager class is actually subclassed and its execute() method overridden to run the handlers sequentially in the same thread, or in multiple threads (e.g., via an ExecutorService), depending on the subclass implementation.

My question is, what would be the fastest (less overhead) way of synchronizing access to the shared Store object inside the run (or call()) method of each Handler object, especially taking into account that, for single-threaded access, that synchronization is redundant (but has to be there, because there are also multi-threaded Manager subclass implementations).

Would, for instance, a synchronized (this.store) {this.store.process()} block be better than, say, using a Lock object from java.util.concurrent, before and after calling this.store.process()? Or would a separate synchronized method inside Handler for each store access be faster? For example, instead of calling this.store.process(), run something like

private synchronized void processStore()
{
    this.store.process();
}

Following is the (sample) source code.

public class Manager
{
    public Manager()
    {
        Store store = new Store(); // Resource to be shared
        List<Handler> handlers = createHandlers(store, 10);

        execute(handlers);
    }

    List<Handler> createHandlers(Store store, int count)
    {
        List<Handler> handlers = new ArrayList<Handler>();

        for (int i=0; i<count; i++)
        {
            handlers.add(new Handler(store));
        }

        return handlers;
    }

    void execute(List<Handler> handlers)
    {
        // Run handlers, either sequentially or concurrently 
    }
}

public class Handler implements Runnable // or Callable
{
    Store store; // Shared resource

    public Handler(Store store)
    {
        this.store = store;
    }

    public void run() // Would be call(), if Callable
    {
        // ...
        this.store.process(); // Synchronization needed
        // ...
        this.store.report();  // Synchronization needed
        // ...
        this.store.close();   // Synchronization needed
        // ...
    }       
}

public class Store
{
    void process() {}
    void report()  {}
    void close()   {}
}
  • 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-08T17:34:53+00:00Added an answer on June 8, 2026 at 5:34 pm

    In general: CAS synchronization < synchronized < Lock in terms of speed. Of course this will depend on the degree of contention and your operating system. I would suggest you try each and determine which is the fastest for your need.

    Java also performs lock elision to avoid locking on objects that are only visible to one thread.

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

Sidebar

Related Questions

I have the following tricky problem: I have implemented a (rather complicated) class which
Code which I have inherited keeps crashing out rather powerfully with the following error
can someone explain the best way to get around the following, rather curious type
Suppose I have the following (rather common) model Client invokes web service request ->
I am rather noob at java jars deployment. I have following scenario: I have
I have following rather simple query select count(*) from tbl t1, tbl t2 For
Being rather new to ASP.MVC I'm looking for a solution to the following routing
I'm rather new to these could someone explain the significance (of the following code)
I'm rather new to jQuery, so I really don't know if the following problem
The question is rather of academic nature... I've got the following architecture: Apache server

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.