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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:52:45+00:00 2026-06-13T16:52:45+00:00

I am recently using Java Threads and I am trying to implement a method

  • 0

I am recently using Java Threads and I am trying to implement a method able to restricts a block of code to be execute only by one thread, lets called “single”.

One naive solution that I already implemented is as follows:

if(readFlag())
{
  synchronized(this)
  {
  flag = false;  
  // do some work
  }
} 

Where :

public Boolean synchronized readFlag() {return flag;}

This naive solution works fine for only one “single” block, but the problem occurs when I try to use multiple “single” code blocks. Since I can not use the same flag, because different threads may whan to enter different “single” code blocks at the same time.

Another solution could be the use of an array of flags variables one for each “single” but this solution would have problems (or require the use of barriers) when executing “single” code blocks inside loops, beside not being very flexible.

My question is:

How could I improve this solution to allow the execution of various “single” code blocks and to allow the execution of the same single code block multiple times (e.g. a single code block inside a loop).

  • 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-13T16:52:47+00:00Added an answer on June 13, 2026 at 4:52 pm

    You can change the object you synchronize on. There is no need to synchronize on this, unless you need to. If you have two blocks that need to be synchronized but that can both be executed at the same time (each by one thread only), you can do something like

    private final Object lock1 = new Object();
    private final Object lock2 = new Object();
    ...
    synchronized(lock1) { // do stuff. the code in lock2 can be executed while another thread is executing this }
    synchronized(lock2) { // do stuff. the code in lock1 can be executed while another thread is executing this }
    

    Also take a look at the ReentrantReadWriteLock for more sophisticated locking (to allow multiple readers but only one writer).

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

Sidebar

Related Questions

I've been recently trying to connect to a hosted MySQL using Java but can't
I have recently created a web project in Java using eclipse. I have a
I have recently just created Java project using Eclipse that requires 2 JAR files
I'm developing my first java application using Eclipse. I've recently needed to adjust the
I am using Eclipse Helios for Java. Everything was working fine until recently. The
I recently started using lcov to visualize my code coverage. It's a great tool.
I've just started using Java EE recently to build webservices. Most of these have
I just started using mock objects (using Java's mockito) in my tests recently. Needless
I just recently started with object oriented programming, using java. Before I was programming
I have recently inherited a large Java Application that has almost no Thread safety

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.