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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:37:37+00:00 2026-06-12T08:37:37+00:00

In my code, I use a synchronized block on the resource set inside another

  • 0

In my code, I use a synchronized block on the resource set inside another synchronized block on the same resource. This did not cause me any problems so far, but I’m curious: what are the consequences if any? Will these synchronized blocks conflict with each other?

Set<myClass> set = Collections.synchronizedSet(new HashSet<myClass>());

synchronized(set) {
    set.add(new myClass());
    ...
    writeSetContentsToFile();
}

public synchronized void writeSetContentsToFile() {
    synchronized(set) {
        ...
    }
}

The set is accessed and changed continously by other threads. I synchronized writeSetContentsToFile(), so there won’t be any conflicts at the file resource, and again, there is a synchronized block inside writeSetContentsToFile(), to make sure no changes are made to the set while iterating over it.

  • 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-12T08:37:38+00:00Added an answer on June 12, 2026 at 8:37 am

    This did not cause me any problems so far, but I’m curious: what are the consequences if any?

    In some ways you answered your own question, it works but its confusing. Its best to simplify the code if you can. You would need to look at the rest of the code to determine whether this is possible.

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

Sidebar

Related Questions

i am trying to use this code. public synchronized void onActivityResult(final int requestCode, int
As i understand the code below, in the synchronized block, this is an instance
I know this code use to work, I must have accidentally changed something when
My code i use to parse HTMl is this below, and the 2nd code
I know the difference between synchronized method and synchronized block but I am not
Refer below code public void acquire(){ synchronized(a){ print(acquire()); try{ //Thread.sleep(5000); synchronized(this){ wait(5000); } print(I
I don't like to lock up my code with synchronized(this) , so I'm experimenting
Are there any synchronizing/reference issues with this code? (Assume that myStrings is already instantiated.)
Using the following code: use LWP::Simple; my $url= http://example.com; my $html= get $url; I
I have the following code: use Imager::Screenshot 'screenshot'; my $img = screenshot(hwnd => 'active',

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.