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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:06:15+00:00 2026-05-27T04:06:15+00:00

I can see how a standard Semaphore Class can be implemented in Java. However,

  • 0

I can see how a “standard” Semaphore Class can be implemented in Java. However, I cant see how to implement a Binary Semaphore Class in Java. How does such implementation work? When should I call the wake and notify methods to wake and stop the threads that are on the semaphores?
I understand what binary semaphores are, but I have no idea of how to code them.

Edit Note: Realize that I said “BINARY” Semaphore class. The standard Semaphore class I already did and I know its correct so the standard Semaphore Class does not interest me.

  • 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-27T04:06:16+00:00Added an answer on May 27, 2026 at 4:06 am

    I think you’re talking about mutex (or mutual exclusion locks). If so, you can use intrinsic locks. This kind of locks in Java act as mutexes, which means that at most one thread may own the lock:

    synchronized (lock) { 
        // Access or modify shared state guarded by lock 
    }
    

    Where lock is a mock object, used only for locking.


    EDIT:

    Here is an implementation for you — non-reentrant mutual exclusion lock class that uses the value zero to represent the unlocked state, and one to represent the locked state.

    class Mutex implements Lock, java.io.Serializable {
    
        // Our internal helper class
        private static class Sync extends AbstractQueuedSynchronizer {
          // Report whether in locked state
          protected boolean isHeldExclusively() {
            return getState() == 1;
          }
    
          // Acquire the lock if state is zero
          public boolean tryAcquire(int acquires) {
            assert acquires == 1; // Otherwise unused
            if (compareAndSetState(0, 1)) {
              setExclusiveOwnerThread(Thread.currentThread());
              return true;
            }
            return false;
          }
    
          // Release the lock by setting state to zero
          protected boolean tryRelease(int releases) {
            assert releases == 1; // Otherwise unused
            if (getState() == 0) throw new IllegalMonitorStateException();
            setExclusiveOwnerThread(null);
            setState(0);
            return true;
          }
    
          // Provide a Condition
          Condition newCondition() { return new ConditionObject(); }
    
          // Deserialize properly
          private void readObject(ObjectInputStream s)
              throws IOException, ClassNotFoundException {
            s.defaultReadObject();
            setState(0); // reset to unlocked state
          }
        }
    
        // The sync object does all the hard work. We just forward to it.
        private final Sync sync = new Sync();
    
        public void lock()                { sync.acquire(1); }
        public boolean tryLock()          { return sync.tryAcquire(1); }
        public void unlock()              { sync.release(1); }
        public Condition newCondition()   { return sync.newCondition(); }
        public boolean isLocked()         { return sync.isHeldExclusively(); }
        public boolean hasQueuedThreads() { return sync.hasQueuedThreads(); }
        public void lockInterruptibly() throws InterruptedException {
          sync.acquireInterruptibly(1);
        }
        public boolean tryLock(long timeout, TimeUnit unit)
            throws InterruptedException {
          return sync.tryAcquireNanos(1, unit.toNanos(timeout));
        }
      }
    

    If you need to know where should you call wait() and notify(), have a look at sun.misc.Unsafe#park(). It is used within java.util.concurrent.locks package (AbstractQueuedSynchronizer <- LockSupport <- Unsafe).

    Hope this helps.

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

Sidebar

Related Questions

I can see some options available: 1) Use PEAR's POP3 class --> tried it,
You can see the way its suppose to work by visiting: link text That
I've made a reinstallation of the buildserver and can no longer see the standard
I have this html <select class=category style=margin-bottom: 5px; onchange=getProducts('standard_product');> and as you can see
I am taking this tutorial. As you can see on this image, the standard
I have a java process running. With jstat I can see the the permgen
I can see people asking all the time whether multiple inheritance should be included
We can see in a directory files ordered by Name in Windows Explorer. If
I can see line numbers in my error logs in our development environment, in
I can see in the postgresql logs that certain simple queries (no joins and

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.