Notes [Java7 Concurrent Programming manual] series catalogue
Brief introduction
This paper continues to study the semaphore semaphore mechanism.
In 3.2 In fact has been explained, before the signal volume of the use of concurrent scenarios do not know, read this chapter to think of some;
The following is a car rental for Lezilai to explain the control of concurrent access. (examples are simple and may not conform to realistic logic)
- The semaphore (non-binary semaphore) is not guaranteed to be synchronous and requires additional synchronization
Example
Scene: There is a taxi company, there are three cars, there are 10 drivers, each driver working time is inconsistent, it can be said that the driver waiting for others to back the car, and then rent a car.
/** * Created by Zhuqiang on 2015/8/17 0017. * * Public class Client { Public Static void Main(string[] args) {Semaphore sh =NewSemaphore (3);//semaphore, allowing 3 threads to rent a vehicle concurrentlyCompany Company =NewCompany ();//Create a companythread[] Drivers =Newthread[Ten]; for(inti =0; i < drivers.length; i++) {Drivers[i] =NewThread (NewDriver (SH, company)); } for(Thread d:drivers) {D.start (); } }}/** Taxi company **/Class Company {Boolean[] Cars =New Boolean[3];//three-car rental status, subscript as the car's numberLock lock =NewReentrantlock ();/** Car Rental * * */ Public int Rent() {intresult =-1; Lock.lock (); for(inti =0; i < cars.length; i++) {if(!cars[i]) {//Get the number of vehicles that have not been usedresult = i; Cars[i] =true;//Set to use status Break; }} System.out.printf ("Driver number:%s: Rent a taxi with number%d: \ n", Thread.CurrentThread (). GetName (), result); Lock.unlock ();returnResult }/** * Return vehicle * * @param num Vehicle number * * Public void Giveback(intnum) {lock.lock (); Cars[num] =false;//Set to idle stateSystem.out.printf ("Driver number:%s: Return a taxi with number%d: \ n", Thread.CurrentThread (). GetName (), num); Lock.unlock (); }}/** Driver * * /Class Driver implements Runnable {Semaphore sh =NULL; Company Company =NULL; Public Driver(Semaphore sh, company Company) { This. sh = sh; This. company = Company; }/** Driver Work * */ @Override Public void Run() {Try{Sh.acquire ();//Get the semaphore intnum = Company.rent ();//Get a car number LongTime = (Long) (Math.random () *Ten);//How much time does the simulation workSystem.out.printf ("Driver number:%s: Taxi with a vehicle number:%d, this time:%d seconds; \ n", Thread.CurrentThread (). GetName (), Num, time); TimeUnit.SECONDS.sleep (time);//Hibernate helper class. Hibernate by specified unitsCompany.giveback (num);//Return vehicle}Catch(Interruptedexception e) {E.printstacktrace (); }finally{sh.release (); } }}
One run Result:
Driver Number: thread-9: The leased number is:0Taxi Driver Number: thread-1: The leased number is:1Taxi Driver Number: thread-2: The leased number is:2Taxi Driver Number: thread-9: The driving vehicle number is:0Taxi, this working time is:8Second; Driver Number: thread-1: The driving vehicle number is:1Taxi, this working time is:4Second; Driver Number: thread-2: The driving vehicle number is:2Taxi, this working time is:2Second; Driver Number: thread-2: The returned number is:2Taxi Driver Number: thread-3: The leased number is:2Taxi Driver Number: thread-3: The driving vehicle number is:2Taxi, this working time is:9Second; Driver Number: thread-1: The returned number is:1Taxi Driver Number: thread-4: The leased number is:1Taxi Driver Number: thread-4: The driving vehicle number is:1Taxi, this working time is:8Second; Driver Number: thread-9: The returned number is:0Taxi Driver Number: thread-5: The leased number is:0Taxi Driver Number: thread-5: The driving vehicle number is:0Taxi, this working time is:8Second; Driver Number: thread-3: The returned number is:2Taxi Driver Number: thread-6: The leased number is:2Taxi Driver Number: thread-6: The driving vehicle number is:2Taxi, this working time is:3Second; Driver Number: thread-4: The returned number is:1Taxi Driver Number: thread-7: The leased number is:1Taxi Driver Number: thread-7: The driving vehicle number is:1Taxi, this working time is:2Second; Driver Number: thread-6: The returned number is:2Taxi Driver Number: thread-8: The leased number is:2Taxi Driver Number: thread-8: The driving vehicle number is:2Taxi, this working time is:8Second; Driver Number: thread-7: The returned number is:1Taxi Driver Number: thread-0: The leased number is:1Taxi Driver Number: thread-0: The driving vehicle number is:1Taxi, this working time is:8Second; Driver Number: thread-5: The returned number is:0Taxi Driver Number: thread-8: The returned number is:2Taxi Driver Number: thread-0: The returned number is:1The taxi
Description:
You can see the result above: it is very correct to have three concurrent taxis in 10 threads; The success point of this example:
1. Number of semaphore resources to <= the number of concurrent access resources (otherwise, a vehicle with a number of 1 will be rented)
2. Concurrent acquisition of shared resources requires additional synchronization
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Notes [Java7 Concurrent Programming Manual]3.3 Multi-copy concurrent access control for resources semaphore