Use of the "Java Concurrency" thread Sync tool semaphore

Source: Internet
Author: User

Semaphore is typically used to limit the number of threads that can access certain resources (physical or logical), and we can set our own maximum traffic. It has two common methods, acquire () and release (), which are licensing and release licenses, respectively.
The official JDK's explanation for semaphore is this:

A count semaphore. Conceptually, semaphores maintain a set of licenses. If necessary, block each acquire () before the license is available, and then obtain the license. Each release () adds a license that may release a blocked fetch. However, instead of using the actual license object, Semaphore only counts the number of available licenses and takes action accordingly. The thread that gets the semaphore can enter the code or wait. Obtain and release access licenses through Acquire () and release ().

My explanation is this:

Semaphore equivalent to a toilet, I can create a few pits when I build a few pits, if now I have built 3 pits, now there are 10 people want to go to the toilet, then each time only 3 people, who first grabbed who went in, came out of a person, the 4th person can go in, This limits the number of toilets, that's the truth. Everyone acquire before the toilet (), if there is a pit, you can enter, not blocked, outside, etc., after the toilet, will release (), release a pit out, to ensure that the next person acquire () when there is a pit.

I think my explanation is better than the official ...

1. Semaphore Basic use

Semaphore is useful for restricting access to resources, such as restricting the number of concurrent accesses to a file, and its rationale is well understood. Here is a sample code for semaphore:

 Public  class semaphoretest {     Public Static void Main(string[] args) {Executorservice service = Executors.newcachedthreadpool ();//Use concurrent libraries to create a cached thread pool        FinalSemaphore SP =NewSemaphore (3);//Create a semaphore semaphore and set the maximum number of concurrent 3        //availablepermits ()//To get the number of accesses currently availableSystem.out.println ("Initialize: currently available"+ (3-Sp.availablepermits () +"a concurrency"));//Create 10 tasks, the cache thread pool above will create 10 corresponding threads to execute         for(intindex =0; Index <Ten; index++) {Final intNO = index;//Record the first few tasksRunnable Run =NewRunnable () {//Specific tasks                 Public void Run() {Try{Sp.acquire ();//Get permissionSystem.out.println (Thread.CurrentThread (). GetName () +"Get a License"+"("+no+"),"+"Remaining:"+ sp.availablepermits ()); Thread.Sleep ( +);//After the visit, remember to release, or you can only print 3 records in the console, then the thread has been blockedSp.release ();//Release licenseSystem.out.println (Thread.CurrentThread (). GetName () +"Release license"+"("+no+"),"+"Remaining:"+ sp.availablepermits ()); }Catch(Interruptedexception e)              {                      }                  }              }; Service.execute (run);//Perform Tasks} service.shutdown ();//close thread pool}}

The code structure is easy to understand, 10 tasks, each time up to 3 threads to perform the task, other threads are blocked. You can view the execution of a thread by printing information:

Initialization: There are currently 0 concurrent
Pool-1-thread-1 get Permission (0), Remaining: 1
Pool-1-thread-3 get Permission (2), remaining: 0
Pool-1-thread-2 get Permission (1), Remaining: 1
Pool-1-thread-1 release License (0), Remaining: 3
Pool-1-thread-4 get Permission (3), Remaining: 1
Pool-1-thread-5 get Permission (4), Remaining: 1
Pool-1-thread-2 Release License (1), Remaining: 3
Pool-1-thread-3 Release License (2), Remaining: 3
Pool-1-thread-6 get Permission (5), remaining: 0
Pool-1-thread-4 Release License (3), Remaining: 2
Pool-1-thread-9 get Permission (8), remaining: 0
POOL-1-THREAD-5 Release License (4), Remaining: 2
POOL-1-THREAD-6 Release License (5), Remaining: 2
Pool-1-thread-8 get Permission (7), remaining: 0
Pool-1-thread-7 get Permission (6), Remaining: 2
Pool-1-thread-8 Release License (7), Remaining: 2
POOL-1-THREAD-10 get Permission (9), Remaining: 2
POOL-1-THREAD-7 Release License (6), Remaining: 2
Pool-1-thread-9 Release License (8), Remaining: 2
POOL-1-THREAD-10 Release License (9), Remaining: 3

From the results, the top three why the rest is not 3,2,1? including below, each time the remaining amount of the release seems to be wrong, is actually right, but the thread is running too fast, the first three is this: because the maximum traffic is 3, so the first three in the print statement before the execution of the Aquire () method, or partially executed, from the above results, Thread 1 is the first to go in, Thread 2 goes in, then threads 1 and 2 start printing, so there are only 1 left, and then thread 3 comes in, printing only 0 left. When released later, there may be more than one release before printing.

2. Semaphore Synchronization Issues

I looked up from the internet, some people said Semaphore realized the synchronization function, I feel wrong, because I wrote a test code to try, and do not solve the concurrency problem, if more than one thread to operate the same data, or need to synchronize their own. Then I checked the official JDK document (to always trust the official document), which says:

Before obtaining an entry, each thread must obtain a license from the semaphore, thereby guaranteeing that the item can be used. After the thread finishes, the item is returned to the pool and the license is returned to that semaphore, allowing other threads to get the item. Note that it is not possible to maintain a synchronous lock when calling acquire () because it prevents the item from being returned to the pool. Semaphores encapsulate the required synchronization to limit access to the pool, which is separate from the synchronization required to maintain the consistency of the pool itself.

This official explanation is very clear, and then I understand that some people on the internet said the realization of synchronization means that the signal itself is packaged in the required synchronization, that is, I got one, others will not be able to get it, I release someone else to get (as I lift the hole in the toilet), But I got it. When I go to operate the public data, the synchronization semaphore for this data operation is no matter, which requires us to synchronize. Write a synchronous test code below:

 Public  class SemaphoreTest2 {    Private Static intdata =0; Public Static void Main(string[] args) {Executorservice service = Executors.newcachedthreadpool ();FinalSemaphore SP =NewSemaphore (3); System.out.println ("Initialize: currently available"+ (3-Sp.availablepermits () +"a concurrency"));//10 Quests         for(intindex =0; Index <Ten; index++) {Final intNO = index; Runnable Run =NewRunnable () { Public void Run() {Try{//Get permissionSp.acquire (); System.out.println (Thread.CurrentThread (). GetName () +"Get a License"+"("+ NO +"),"+"Remaining:"+ sp.availablepermits ());//Implement synchronization                        synchronized(Semaphoretest2.class) {System.out.println (Thread.CurrentThread (). GetName () +"Perform data self-increment before: Data="+ data);                            data++; System.out.println (Thread.CurrentThread (). GetName () +"Perform data increment: Data="+ data);                        } sp.release (); System.out.println (Thread.CurrentThread (). GetName () +"Release license"+"("+ NO +"),"+"Remaining:"+ sp.availablepermits ()); }Catch(Interruptedexception e)            {                    }                }            };        Service.execute (run);    } service.shutdown (); }}

Look at the results of the operation (part):

Initialization: There are currently 0 concurrent
Pool-1-thread-2 get Permission (1), Remaining: 0
Pool-1-thread-2 perform data self-increment before: data=0
Pool-1-thread-3 get Permission (2), remaining: 0
Pool-1-thread-1 get Permission (0), remaining: 0
Pool-1-thread-2 perform data self-increment: data=1
Pool-1-thread-3 perform data self-increment before: data=1
Pool-1-thread-3 perform data self-increment: data=2
Pool-1-thread-1 perform data self-increment before: data=2
Pool-1-thread-7 get Permission (6), Remaining: 1
Pool-1-thread-3 Release License (2), Remaining: 2
Pool-1-thread-1 perform data self-increment: data=3

As can be seen from the results, each thread will not be affected by other threads before or after the operation of the data, but other threads can obtain the license, get the license and then be blocked out, waiting for the current thread to operate. It is also possible for other threads to release the license when the current thread is manipulating data because it does not conflict at all.
If you remove the above Sync code block, try again what would be a messy result (part):

Initialization: There are currently 0 concurrent
Pool-1-thread-3 get Permission (2), remaining: 0
Pool-1-thread-2 get Permission (1), Remaining: 0
Pool-1-thread-3 perform data self-increment before: data=0
Pool-1-thread-2 perform data self-increment before: data=0
Pool-1-thread-1 get Permission (0), remaining: 0
Pool-1-thread-3 perform data self-increment: data=1
Pool-1-thread-2 perform data self-increment: data=2
Pool-1-thread-7 get Permission (6), remaining: 0
Pool-1-thread-1 perform data self-increment before: data=2
Pool-1-thread-8 get Permission (7), remaining: 0
Pool-1-thread-7 perform data self-increment before: data=2
Pool-1-thread-2 Release License (1), Remaining: 1
Pool-1-thread-7 perform data self-increment: data=4

From the results, it is clear that threads 2 and 3 are all in, then the initial data is 0, thread 3 has been increased, printing 1 is fine, but thread 2? Also increased from a bit, but printed out 2. That is, thread 2 before and after the operation of the data, the data has been modified by thread 3, once again to prove that Semaphere does not realize the synchronization of the common data, in the operation of public data, we need to implement ourselves.
Semaphere If the semaphore is set to 1, then it means that only one thread at a time to operate the task, then there is no thread safety problem, so if the set signal volume of 1, you can remove the synchronized, but the efficiency is not.
The use of Semaphere summed up so much!
  

Related reading: http://blog.csdn.net/column/details/bingfa.html

-Willing to share and progress together!
-More articles please see: http://blog.csdn.net/eson_15

Java Concurrency thread Synchronization tool semaphore use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.