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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:34:12+00:00 2026-05-21T07:34:12+00:00

I am learning synchronized blocks with locks.I want to know the difference between this

  • 0

I am learning synchronized blocks with locks.I want to know the difference between this lock and some third party lock that provided in the program.

public class NewThread extends Thread {
StringBuffer sb;
NewThread(StringBuffer sb){
    this.sb=sb;
}
public void run(){
    synchronized(this.sb){
        for(int i=0;i<1000;i++){
            System.out.print(sb);
               try{
             Thread.sleep(5*60);
        }
        catch(Exception e){}
        }
       char c = this.sb.charAt(0);
       this.sb.setCharAt(0, ++c);
    }
}
public static void main(String[] args){
    StringBuffer sb=new StringBuffer("A");
    NewThread nt=new NewThread(sb);
    NewThread nt1=new NewThread(sb);
    NewThread nt2=new NewThread(sb);
    nt.start();
    nt1.start();
    nt2.start();
}

}

If i am going to put

public void run(){
    synchronized(this){
        for(int i=0;i<1000;i++){
            System.out.print(sb);
               try{
             Thread.sleep(5*60);
        }
        catch(Exception e){}
        }
       char c = this.sb.charAt(0);
       this.sb.setCharAt(0, ++c);
    }
}

here in the above run method i gave this in the synchronized block …i want the difference between them

i am having one more question ,if we are giving a lock object in synchronized block and we are not using that object inside that block then does we observe any thing specific compared to normal block

  • 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-21T07:34:13+00:00Added an answer on May 21, 2026 at 7:34 am

    If you have concurrent development experience with general OS API, e.g. pthread library in Linux, you may have a knowledge that we should use lock or other data structure to synchronize processes/threads accessing critical section(where shared object may be modified).

    Java use lock to implement synchronization block. Synchronization block is a kind of mechanism(called monitor in operating system) encapsulating tedious operation related to lock. Each object has a lock in java. When synchronized, lock in shared object is locked at first(also we can say other process require lock of the shared object). If some thread fail to acquire the lock, that means some other thread is now holding the lock, it must wait until other thread release the lock and re-acquire again until it hold the lock and then enter the critical section.

    The first code snippet use lock in instance of StringBuffer, i.e. sb, each thread will try to get sb’s lock(invoke lock_of_sb.lock() before running the code. Only that acquired sb’s lock successfully can eventually execute the code.

    As to the second code, which is equivalent to

    public synchronized void run(){
      for(int i=0;i<1000;i++){
          System.out.print(sb);
             try{
           Thread.sleep(5*60);
      }
      catch(Exception e){}
      }
      char c = this.sb.charAt(0);
      this.sb.setCharAt(0, ++c);
    }
    

    I don’t think it behaves as you expected. It acquire lock in this object, however, this object is never shared. So the shared sb is exposed into the critical section without any synchronization.

    The link will give you another working way to achieve synchronization.How to synchronize static method in java Although the question itself is about static method, it also works well to instance member method.

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

Sidebar

Related Questions

Im learning lisp and im pretty new at this so i was wondering... if
I remember first learning about vectors in the STL and after some time, I
I'm learning about deadlocks in Java, and there's this sample code from Sun's official
In learning Core Animation, I learned very quickly that if you don't do it
Still learning lxml. I discovered that sometimes I cannot get to the text of
Still learning JSP Web Applications here. I have been doing this for a while
I`m learning programming languages. And I decide that I need to lear a new
Im learning Spring (2 and 3) and i got this method in a ClientDao
Learning WPF nowadays. Found something new today with .Net dependency properties. What they bring
Learning a little about T-SQL, and thought an interesting exercise would be to generate

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.