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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:21:06+00:00 2026-05-29T05:21:06+00:00

I have an observer that can be called several times. If the onChange method

  • 0

I have an observer that can be called several times. If the onChange method of the observer is still running a second call should wait and the following calls should be ignored.

I used this:

Semaphore semaphore = new Semaphore(2,false);
...
public void onChange() {
    if (!semaphore.tryAcquire()) return;
    synchronized (semaphore) { 
        // do your stuff
        semaphore.release();
    }
}

Is this ok? Is there a better way?

  • 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-29T05:21:07+00:00Added an answer on May 29, 2026 at 5:21 am

    Your logic looks good to me. As I read it 2 threads will be able to acquire the Semaphore in any order. Any other threads trying to acquire will return and be ignored.

    The first thread to do the synchronized will block the other one so only one thread will be working and one (max) will be waiting. As soon as the job has been completed and release() is called then another thread will be allowed into the semaphore. As soon as a the synchronized block completes, the first waiting thread will start to work.

    Another way to do it would be with two Semaphore objects with the 2nd one doing a acquire instead of a tryAcquire. But your way should work.

    As @herschel pointed out, I would do a try/finally around “do your stuff” to guarantee that the semaphore is released. Something like:

    ...
    synchronized (semaphore) {
        try {
            // do your stuff
        } finally {
            semaphore.release();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom observer in Magento 1.6.2.0 that is called when a CMS
I have a content observer that should be notified when one of the contacts
Using Observer pattern. I have a class, called Monitor for example, that is monitoring
We have Core2 machines (Dell T5400) with XP64. We observe that when running 32-bit
I have made a generic Observer interface and an Observable class, but can't compile
I have observed that windowWillClose is called when the user closes a window but
I have an app that needs to wait for some unknown amount of time.
I have this observer that watches the UISwitch for a change in value: [cell.switcher
can you help me understand the observeValueForKeyPath method : here it seems that observeValueForKeyPath
I have written an iPhone and iPad app that should streams e.g. 4 videos

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.