This article mainly introduces some Java The thread synchronization tool class, does not carry on the concrete explanation, when has the need, may again to combine the example study.
Beacon (Semaphore)
Examples of application scenarios:
For example, the company's punch-card system, if there is a punch, then only one person can punch in, the rest of the people are blocked, punch out after the next person can be clocked. If there are 3 punch machines, then 3 people or less than three people are allowed to punch in one time, and the rest of the people have to wait for the punch machine to be idle to continue to punch in.
Results:
has entered 1 threads and has access to 2
has entered 2 threads and has access to 1
has entered 3 threads and has access to 0
1 out of spare
has entered 4 threads and has access to 0
has entered 6 threads and has access to 0
0 out of spare
1 out of spare
has entered 5 threads and has access to 0
0 out of spare
has entered 8 threads and has access to 0
1 out of spare
has entered 7 threads and has access to 0
has entered 9 threads and has access to 0
0 out of spare
1 out of spare
3 out of spare
3 out of spare
As you can see from the printout, although you can enter three threads at a time, it is not to wait for all three threads to be empty to enter other threads. Instead, as long as there is a free, waiting thread is immediately mended. How similar it is to real-life scenarios. This shows that the traffic lights used to control the thread is very human.
Obstacle Device (Cyclicbarrier)
Examples of application scenarios:
For example, the company received a project (main thread), some people develop the foreground (thread 1), some people develop the background (thread 2), the foreground and the background can not be developed at the same time, but only the foreground and the background are developed to be submitted to the customer. Cyclicbarrier, then, implements the ability to block threads that have completed tasks.
Results:
The front desk is developing!
Backstage is developing!
Front desk development completed
Background development completed
Two project teams have been developed and tested.
Test pass, deliver to Customer
Synchroized, Lock, and Semaphroe all control the start of thread execution, and Cyclicbarrier is the completion of the thread.
Sync Counter (countdownlatch)
Examples of application scenarios:
The effect is to allow one or more threads to wait until a set of operations that are being performed in another thread is completed.
Results:
Ready to go!
Athlete number 7th arrives at the finish line!
Athlete number 9th arrives at the finish line!
Athlete number 6th arrives at the finish line!
Athlete number 1th arrives at the finish line!
Athlete number 3rd arrives at the finish line!
Athlete number 4th arrives at the finish line!
Athlete Number No. 0 arrives at the finish line!
Athlete number 8th arrives at the finish line!
Athlete number 2nd arrives at the finish line!
Athlete number 5th arrives at the finish line!
End!
In contrast (cyclicbarrier), a barrier can only control the end of a thread. However, the counter (Countdownlatch) can be combined with multiple counters, both to control the start of the thread and to control the end of the thread.
inter-thread data exchange (Exchanger)
Examples of application scenarios:
The functionality that is implemented is the exchange of data between threads. However, it is important to note that data exchange can only be two threads.
Results:
Xu Yang has five dollars.
The store has beverage
Start trading ...
The store gets: five dollars
Xu Yang get beverage
Summary:
These are some of the threads of the synchronization tool class, they have each of the application scenarios, in concurrent programming, we can apply these tools to better achieve our business needs.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
JAVA concurrent Programming-Thread Synchronization tool Class (12)