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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:22:33+00:00 2026-05-20T13:22:33+00:00

I have a strange synchronization architecture going on and I am looking for an

  • 0

I have a strange synchronization architecture going on and I am looking for an elegant solution. I already had a solution but I cannot say anything about its validity and its also a little ugly. So here is the problem, hopefully someone can help me.

There are 2 groups of tasks that can be started and run. Each task has its own thread. These two groups both extend from one super class that takes care of the synchronization part. I will call these two groups Group A and Group B for simplicity.

Conditions:

  • If there are only Group B tasks running then they can run at the same time and they do not interfere with each other.

  • If a Group A task is started* then the constructor for a Group B task should fail with an exception. Any number of Group A tasks can be created even if a Group A task is already running

  • A Group A task cannot execute until all current Group B tasks have finished. (* from above)

  • Only 1 Group A task can run at a time. They must be queued. (It is optional to block Group B tasks between the running of the two Group A tasks as long as the previous conditions still apply)

I believe my method works but I do not like the way it works because of the many different synchronization points that it uses, and the fact that I have a sleep while waiting for the counter. Anyway the code is below

public abstract class LockableTask<Params> extends AsyncTask {

private final boolean groupA;
private static Boolean locked = false;
private static final Semaphore semLock = new Semaphore(1);
private static int count = 0;

public LockableTask(boolean groupA) {
    this.groupA = groupA;
    synchronized (locked) {
        if (locked && !groupA) {
            throw new InputException("We are locked, please wait");
        }
    }
}

@Override
protected final AsyncReturn doInBackground(Params... params) {
    if (!groupA) {
        synchronized (locked) {
            count++;
        }
    }

    try {
        if (groupA) {
            semLock.acquireUninterruptibly();
            synchronized (locked) {
                locked = true;
            }

            while (true) {
                synchronized (locked) {
                    if (count == 0) {
                        break;
                    }
                }

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {}
            }
        }
        return runInBackground(params);
    } finally {
        synchronized (locked) {
            if (groupA) {
                locked = false;
            } else {
                count--;
            }
        }

        if (groupA) {
            semLock.release();
        }
    }
}

protected abstract AsyncReturn runInBackground(Params... params);
}

If someone has a nicer solution even if just barely nicer that would be great

  • 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-20T13:22:34+00:00Added an answer on May 20, 2026 at 1:22 pm

    If you have an upper bound on the number of simultaneous Group B tasks (even if it’s very large), you should be able to achieve the rules you describe with a single semaphore.

    int UPPER_BOUND=1000000;
    Semaphore s=new Semaphore(UPPER_BOUND);
    

    Group A task:

    s.acquireUninterruptibly(UPPER_BOUND);
    ......
    s.release(UPPER_BOUND);
    

    Group B task:

    if (!s.tryAcquire())
        throw new WhateverException("Upper bound of Bs reached or A running");
    ......
    s.release();
    

    End result: whilst any group B are running, A cannot acquire the number of permits it requires. Once it does, no B can acquire a permit and neither can any other A.

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

Sidebar

Related Questions

I have a strange issue: I am using SPContext.Current.Web in a .aspx page, but
I have a strange problem with VS2008. My solution has 8 projects (all c#)
i have strange behaviour of git - push is working, but clone is not
I have some strange problem. I have a solution with the following structure http://i33.tinypic.com/10fbzbq.jpg
I have strange problem with sharepoint and ajax functionality. We have an UpdatePanel placed
I have this strange call stack and I am stumped to understand why. It
I have a strange requirement to ship an application without bundling .Net framework (to
We have very strange problem, one of our applications is continually querying server by
I have a strange, sporadic issue. I have stored procedure that returns back 5
I have this strange issue with my web app. You see, I'm using jQuery

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.