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

  • Home
  • SEARCH
  • 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 5959443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:41:08+00:00 2026-05-22T18:41:08+00:00

Today I was faced with the method constructServiceUrl() of the org.jasig.cas.client.util.CommonUtils class. I thought

  • 0

Today I was faced with the method constructServiceUrl() of the org.jasig.cas.client.util.CommonUtils class. I thought he was very strange:

final StringBuffer buffer = new StringBuffer();

synchronized (buffer)
{
    if (!serverName.startsWith("https://") && !serverName.startsWith("http://"))
    {
        buffer.append(request.isSecure() ? "https://" : "http://");
    }

    buffer.append(serverName);
    buffer.append(request.getRequestURI());

    if (CommonUtils.isNotBlank(request.getQueryString()))
    {
        final int location = request.getQueryString().indexOf(
                artifactParameterName + "=");

        if (location == 0)
        {
            final String returnValue = encode ? response.encodeURL(buffer.toString()) : buffer.toString();

            if (LOG.isDebugEnabled())
            {
                LOG.debug("serviceUrl generated: " + returnValue);
            }

            return returnValue;
        }

        buffer.append("?");

        if (location == -1)
        {
            buffer.append(request.getQueryString());
        }
        else if (location > 0)
        {
            final int actualLocation = request.getQueryString()
                    .indexOf("&" + artifactParameterName + "=");

            if (actualLocation == -1)
            {
                buffer.append(request.getQueryString());
            }
            else if (actualLocation > 0)
            {
                buffer.append(request.getQueryString().substring(0, actualLocation));
            }
        }
    }
}

Why did the author synchronizes a local variable?

  • 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-22T18:41:08+00:00Added an answer on May 22, 2026 at 6:41 pm

    This is an example of manual “lock coarsening” and may have been done to get a performance boost.

    Consider these two snippets:

    StringBuffer b = new StringBuffer();
    for(int i = 0 ; i < 100; i++){
        b.append(i);
    }
    

    versus:

    StringBuffer b = new StringBuffer();
    synchronized(b){
      for(int i = 0 ; i < 100; i++){
         b.append(i);
      }
    }
    

    In the first case, the StringBuffer must acquire and release a lock 100 times (because append is a synchronized method), whereas in the second case, the lock is acquired and released only once. This can give you a performance boost and is probably why the author did it. In some cases, the compiler can perform this lock coarsening for you (but not around looping constructs because you could end up holding a lock for long periods of time).

    By the way, the compiler can detect that an object is not “escaping” from a method and so remove acquiring and releasing locks on the object altogether (lock elision) since no other thread can access the object anyway. A lot of work has been done on this in JDK7.


    Update:

    I carried out two quick tests:

    1) WITHOUT WARM-UP:

    In this test, I did not run the methods a few times to “warm-up” the JVM. This means that the Java Hotspot Server Compiler did not get a chance to optimize code e.g. by eliminating locks for escaping objects.

    JDK                1.4.2_19    1.5.0_21    1.6.0_21    1.7.0_06
    WITH-SYNC (ms)         3172        1108        3822        2786
    WITHOUT-SYNC (ms)      3660         801         509         763
    STRINGBUILDER (ms)      N/A         450         434         475
    

    With JDK 1.4, the code with the external synchronized block is faster. However, with JDK 5 and above the code without external synchronization wins.

    2) WITH WARM-UP:

    In this test, the methods were run a few times before the timings were calculated. This was done so that the JVM could optimize code by performing escape analysis.

    JDK                1.4.2_19    1.5.0_21    1.6.0_21    1.7.0_06
    WITH-SYNC (ms)         3190         614         565         587
    WITHOUT-SYNC (ms)      3593         779         563         610
    STRINGBUILDER (ms)      N/A         450         434         475
    

    Once again, with JDK 1.4, the code with the external synchronized block is faster. However, with JDK 5 and above, both methods perform equally well.

    Here is my test class (feel free to improve):

    public class StringBufferTest {
    
        public static void unsync() {
            StringBuffer buffer = new StringBuffer();
            for (int i = 0; i < 9999999; i++) {
                buffer.append(i);
                buffer.delete(0, buffer.length() - 1);
            }
    
        }
    
        public static void sync() {
            StringBuffer buffer = new StringBuffer();
            synchronized (buffer) {
                for (int i = 0; i < 9999999; i++) {
                    buffer.append(i);
                    buffer.delete(0, buffer.length() - 1);
                }
            }
        }
    
        public static void sb() {
            StringBuilder buffer = new StringBuilder();
            synchronized (buffer) {
                for (int i = 0; i < 9999999; i++) {
                    buffer.append(i);
                    buffer.delete(0, buffer.length() - 1);
                }
            }
        }    
    
        public static void main(String[] args) {
    
            System.out.println(System.getProperty("java.version"));
    
            // warm up
            for(int i = 0 ; i < 10 ; i++){
                unsync();
                sync();
                sb();
            }
    
            long start = System.currentTimeMillis();
            unsync();
            long end = System.currentTimeMillis();
            long duration = end - start;
            System.out.println("Unsync: " + duration);
    
            start = System.currentTimeMillis();
            sync();
            end = System.currentTimeMillis();
            duration = end - start;
            System.out.println("sync: " + duration);
    
            start = System.currentTimeMillis();
            sb();
            end = System.currentTimeMillis();
            duration = end - start;
            System.out.println("sb: " + duration);  
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today I have faced very strange behavior of sqlite3_finalize statement. I am using sqlite
Today I faced a strange problem in C#. I have an ASP.NET page where
Today we faced a quite simple problem that were made even simpler by the
Today when I was in computer organization class, teacher talked about something interesting to
Today I faced an interesting issue. I've been having an inheritance hierarchy with Hibernate
Today I faced one question in interview. Is it possible to apply inheritance concept
Today my Visual studio couldn't help me by auto complete so I thought that
Today I was faced with a challenge to create different behaviors for my shopping
Today I faced an error due to which my Android application is getting by
Today I faced a problem that there is one memory leak in my iphone

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.