Preliminary tutorial on multi-thread synchronization-design and use of Barrier

Source: Internet
Author: User


Barrier is a frequently used synchronization tool in multi-threaded programming. It is especially used for synchronization in the computing process of large data volumes. This article takes the Barrier Implementation of the concurrent toolkit of Doug Lea, which is widely used as an example to discuss it. In Doug Lea's concurrent toolkit, Barrier is an interface that provides two Barrier implementations in the concurrent package: javasicbarrier and Rendezvous. The following is the definition of the Barrier interface:

  1. Public InterfaceBarrier {
  2. /**
  3. * Return the number of parties that must meet per barrier
  4. * Point. The number of parties is always at least 1.
  5. **/
  6. Public IntParties ();
  7. /**
  8. * Returns true if the barrier has been compromised
  9. * By threads leaving the barrier before a synchronization
  10. * Point (normally due to interruption or timeout ).
  11. * Barrier methods in implementation classes throw
  12. * Throw BrokenBarrierException upon detection of breakage.
  13. * Implementations may also support some means
  14. * To clear this status.
  15. **/
  16. Public BooleanBroken ();
  17. }


The method in the Barrier interface is very simple. parties () returns the number of threads to be synchronized at the Barrier. broken () returns a flag indicating whether the release has been destroyed. The Barrier interface does not provide a Barrier method, but is provided in the Barrier Implementation of c and Rendezvous. You may wonder why these methods are not provided in the Barrier interface? Because of the differences in these implementations, it is difficult to extract a shared method signature from these implementations. For example, the method for adding javasicbarrier to a barrier (),

  1. // Javasicbarrier. java
  2. Public IntBarrier ()Throws InterruptedException, BrokenBarrierException {
  3. ReturnDoBarrier (False, 0 );
  4. }
  5. Protected Synchronized IntDoBarrier (BooleanTimed,LongMsecs)
  6. Throws InterruptedException, TimeoutException, BrokenBarrierException {
  7. IntIndex = -- count _;
  8. If(Broken _){
  9. Throw NewBrokenBarrierException (index );
  10. }
  11. Else If(Thread. Interrupted ()){
  12. Broken _ =True;
  13. Policyall ();
  14. Throw New InterruptedException();
  15. }
  16. Else If(Index = 0 ){// Tripped
  17. Count _ = parties _;
  18. ++ Resets _;
  19. Policyall ();
  20. Try{
  21. If(BarrierCommand _! =Null)
  22. BarrierCommand _. run ();
  23. Return0;
  24. }
  25. Catch(RuntimeExceptionEx ){
  26. Broken _ =True;
  27. Return0;
  28. }
  29. }
  30. Else If(Timed & msecs <= 0 ){
  31. Broken _ =True;
  32. Policyall ();
  33. Throw NewTimeoutException (msecs );
  34. }
  35. Else{// Wait until next reset
  36. IntR = resets _;
  37. LongStartTime = (timed )?System. CurrentTimeMillis (): 0;
  38. LongWaitTime = msecs;
  39. For(;;){
  40. Try{
  41. & N

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.