Java Semaphore Semaphore

   public static void main(String[] args) {

                // thread pool 

                ExecutorService Exec = Executors.newCachedThreadPool ();

                // only five threads access

                final Semaphore semp = new Semaphore(5);

                 // simulate 20 Client Access

                 for (int index = 0; index < 20; index++) {

                              final int NO = index;

                              Runnable run = new Runnable() {

                                                 public void run() {

                                                            try {

                                                                    // get license

                                                                    semp.acquire ();

                                                                    System.out.println("Accessing: " + NO);

                                                                    Thread.sleep((long) (Math.random() * 10000));

                                                                    // access after release

                                                                    semp.release();

                                                                    System.out.println("-----------------"+semp.availablePermits());

                                                            } catch (InterruptedException e) {

                                                                    e.printStackTrace ();

                                                            }

                                                  }

                                      };

                      exec.execute(run);

             }

             // exit the thread pool

             exec.shutdown();

       }
View Code

 

Semaphore's role: in java, using the synchronized keyword and Lock locks for concurrency control access to resources, at the same time allowing only a single thread to enter the critical section to access resources (except read lock), the main purpose is to control like this to address inconsistent data concurrently by multiple threads of the same resources caused problems. In another scenario, there are multiple copies of a resource available for use, such as printers rooms have multiple printers, multiple pit toilets are available simultaneously, in this case, Java provides additional concurrent access control - - concurrent multiple copies resource access control, today semaphore semaphore study that is one of them. On the realization of the principle of Semaphore: Semaphore is used to protect one or more access to shared resources, internal Semaphore maintains a counter, whose value is the number of shared resources can be accessed. One thread to access shared resources, to obtain the semaphore if the semaphore counter value is greater than 1, meaning that there is a shared resource can be accessed, so the counter value minus 1, and then access shared resources. If the counter value is 0, the thread goes to sleep. When a thread is finished using a shared resource, the semaphore is released, and the internal semaphore counter is incremented by 1, before entering the dormant thread wakes up and tried again to get the semaphore. Administrators like a toilet, standing in the doorway, only the toilet there is space, open the door and allow the number of empty side of the same amount of people into the toilet. After more than one person enters the toilet, corresponding to N allocated to the individual using the N vacancies. To prevent more than one person to compete with a flanker while still using locks internally to synchronize access control resources. Semaphore use: when you need to build a parameter to specify the number of Semaphore use of shared resources, after the completion of construction is to obtain Semaphore Semaphore, after use of shared resources released Semaphore.

On the realization of the principle of Semaphore: Semaphore is used to protect one or more access to shared resources, internal Semaphore maintains a counter, whose value is the number of shared resources can be accessed. One thread to access shared resources, to obtain the semaphore if the semaphore counter value is greater than 1, meaning that there is a shared resource can be accessed, so the counter value minus 1, and then access shared resources. If the counter value is 0, the thread goes to sleep. When a thread is finished using a shared resource, the semaphore is released, and the internal semaphore counter is incremented by 1, before entering the dormant thread wakes up and tried again to get the semaphore. Administrators like a toilet, standing in the doorway, only the toilet there is space, open the door and allow the number of empty side of the same amount of people into the toilet. After more than one person enters the toilet, corresponding to N allocated to the individual using the N vacancies. To prevent more than one person to compete with a flanker while still using locks internally to synchronize access control resources. Semaphore use: when you need to build a parameter to specify the number of Semaphore use of shared resources, after the completion of construction is to obtain Semaphore Semaphore, after use of shared resources released Semaphore.

 
Semaphore semaphore = new Semaphore(10,true);

 

       semaphore.acquire();

 

    1. //do something here 
      semaphore.release();
       
       
       
       
       
       
       
 
Finally, Semaphore in addition to control concurrent access to multiple copies of control resources, can also be achieved concurrent access control functions like synchronized keyword and Lock locks a binary semaphore. 

Semaphore maintain the current number of access, it provides synchronization mechanisms to control the number of simultaneous access

Demo stated above in the Semaphore one of only five licenses, and there are 20 threads to access the resources, access permissions acquired and released by acquire () and release ().

 


----------------
Disclaimer: This article is CSDN blogger "Dayu if Chi _" in the original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/zbc1090549839/article/details/53389602

Guess you like

Origin www.cnblogs.com/jerrys/p/12236958.html