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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:15:30+00:00 2026-05-30T18:15:30+00:00

Would like to post some doubts regarding Thread synchronization. I understood the concepts of

  • 0

Would like to post some doubts regarding Thread synchronization. I understood the concepts of
synchronization. But when I implemented a sample java program through multithreading, I could not achieve the result what I want. But finally I could achieve the result by changing one line of code.But I would like to know whats wrong with the below code.

Here when I use synchronized block, synchnonized(SharedResource.class) I could achieve the result what I want.
why it is not working for synchonized method and synchronized(this) block?

public class SharedResource {   
    public  synchronized void access(String name){      

            System.out.println(name+" :accessed shared resoure");
            System.out.println(name+" doing his job: ");
            for(int i = 0; i < 5;i++){
                try {
                    System.out.println(name+": "+ i);
                    Thread.sleep(500);
                } catch (InterruptedException e) {              
                    e.printStackTrace();
                }
            }
            System.out.println(name+" :finished doing his job..");

        }
}


public class SharedAccessThread implements Runnable {

    private String name ;
    public SharedAccessThread(String name) {
            this.name = name ;
    }
    @Override
    public void run() {
        SharedResource resource = new SharedResource();
        resource.access(Thread.currentThread().getName());

    }

}



public class MultiThreading {

    public static void main(String[] args) {

        SharedAccessThread thread = new SharedAccessThread(Thread.currentThread().getName());

        Thread t1 = new Thread(thread);
        t1.setName("A");
        Thread t2 = new Thread(thread);
        t2.setName("b");
        Thread t3 = new Thread(thread);
        t3.setName("C");

        t1.start();
        t2.start();
        t3.start();

    }

}

   The expected out put is below:

A :accessed shared resoure
A doing his job: 
A: 0
A: 1
A: 2
A: 3
A: 4
A :finished doing his job..
C :accessed shared resoure
C doing his job: 
C: 0
C: 1
C: 2
C: 3
C: 4
C :finished doing his job..
b :accessed shared resoure
b doing his job: 
b: 0
b: 1
b: 2
b: 3
b: 4
b :finished doing his job..

But it gives in a shuffled manner. That means A accessed shared resource..B doing his job…,etc which I dont want.

  • 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-30T18:15:31+00:00Added an answer on May 30, 2026 at 6:15 pm

    The problem is that you are creating a new SharedResource inside each thread:

    @Override
    public void run() {
        SharedResource resource = new SharedResource();
        resource.access(Thread.currentThread().getName());
    
    }
    

    Therefore, synchronizing on this or on the method itself (which is the same as synchronizing on this) causes each thread to see a different lock and so it has no effect.

    Synchronizing on the class itself works because it functions as a global lock, since the class itself is the same for all threads.

    To be able to synchronize on this you should make the resource a static member for example:

    public class SharedAccessThread implements Runnable {
    
        private String name;
        private static SharedResource resource = new SharedResource();
    
        public SharedAccessThread(String name) {
                this.name = name ;
        }
        @Override
        public void run() {          
            resource.access(Thread.currentThread().getName());   
        }    
    }
    

    This guarantees that all your threads see the same instance of SharedResource.

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

Sidebar

Related Questions

I would like to invoke a webservice via Android. I need to POST some
I would like to post-process my app.config file and perform some token replacements after
I'm trying to do something like this post but with Excel VBA. I would
i am having some tweets like things and i would like to post that
I would like post some message as another user (not currently using the app).
I would like to perform some post processing on the html that the view
I would like to POST an entity as follows POST /example.org/MyEntity/100 Based on the
I would like to implement a post build event that performs the following actions
I would like to know how to send a post request in curl and
I'm using ILMerge in my post build step. For interoperability reason I would like

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.