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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:17:50+00:00 2026-06-15T14:17:50+00:00

I wrote a simple class that uses AbstractQueuedSynchronizer. I wrote a class that represents

  • 0

I wrote a simple class that uses AbstractQueuedSynchronizer. I wrote a class that represents a “Gate”, that can be passed if open, or is blocking if closed. Here is the code:

public class GateBlocking {

  final class Sync extends AbstractQueuedSynchronizer {
    public Sync() {
      setState(0);
    }

    @Override
    protected int tryAcquireShared(int ignored) {
      return getState() == 1 ? 1 : -1;
    }

    public void reset(int newState) {
      setState(newState);
    }
  };

  private Sync sync = new Sync();

  public void open() {
    sync.reset(1);
  }

  public void close() {
    sync.reset(0);
  }

public void pass() throws InterruptedException {
    sync.acquireShared(1);
  }

};

Unfortunately, if a thread blocks on pass method because gate is closed and some other thread opens the gate in meantime, the blocked one doesn’t get interrupted – It blocks infinitely.
Here is a test that shows it:

public class GateBlockingTest {

    @Test
    public void parallelPassClosedAndOpenGate() throws Exception{
        final GateBlocking g = new GateBlocking();
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                    g.open();
                } catch (InterruptedException e) {
                }
            }
        });


        t.start();
        g.pass();
    }
}

Please help, what should I change to make the gate passing thread acquire the lock successfully.

  • 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-15T14:17:51+00:00Added an answer on June 15, 2026 at 2:17 pm

    It looks like setState() only changes the state, but doesn’t notify blocked threads about the change.

    Therefore you should use acquire/release methods instead:

    @Override
    protected boolean tryReleaseShared(int ignored) {
        setState(1);
        return true;
    }
    ...
    public void open() {
       sync.releaseShared(1);
    }
    

    So, overall workflow of AbstractQueuedSynchronizer looks like follows:

    • Clients call public acquire/release methods

    • These methods arrange all synchronization functionality and delegate actual locking policy to protected try*() methods

    • You define your locking policy in protected try*() methods using getState()/setState()/compareAndSetState()

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

Sidebar

Related Questions

I wrote simple class that on the start it just increase the value of
i wrote a simple class that derives QPushButton, and tried to apply stylesheet on
I want to write a simple class (PHP5) that can 'run' an unknown amount
Below is a very simple bit of code that mimics a class structure in
I wrote these two classes that simple encrypt and decrypt strings: Encode.php class Encode
I wrote a flesch reading program that uses another class. I was under the
I'm writing an application using gtkmm. I wrote a simple widget class, that I
I wrote a simple Webservice as below in Netbeans. @WebService() public class CRSEncryptString {
I have written a ListActivity class that uses a custom ArrayAdapter and Holders. I
We have an app that uses simple one way binding with a GridView to

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.