Machine-translated by Google. Questions are authored in English.
Top

Discussion

Sample67 is run - what will be the output?

import java.util.concurrent.Semaphore;

public class Sample67
{
    public static void main(String[] args) throws InterruptedException
    {
        Semaphore permits = new Semaphore(1);
        permits.acquire();
        System.out.print(permits.availablePermits() + " ");
        permits.release();
        System.out.print(permits.availablePermits());
    }
}

  • A.1 1
  • B.0 0
  • C.0 1
  • D.1 0

Answer: C

acquire takes a permit and blocks if none is available, so the count drops to zero; release returns one, taking it back to one. A semaphore counts rather than tracking an owner, which is why release may be called by a thread that never acquired.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.