Java – why doesn’t the acquire () method in semaphores have to be synchronized?

I'm using java to enter semaphores and reading this article http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Semaphore.html. The only thing I don't get is why the acquire () method is not used in the context of synchronization Take an example of the website above:

They created a semaphore:

private Semaphore semaphore = new Semaphore(100);

And obtain such a license:

semaphore.acquire();

Now, is it possible for two or more threads to try to get () at the same time? If so, there will be a problem with counting

Or, does the semaphore itself process synchronization?

Solution

Yes, basically Semaphores are thread safe, as described in the Javadoc:

java. util. Most operations on objects in the concurrent package are thread safe More details are provided at the bottom of the package Javadoc

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>