Java multithreading problem-synchronizing icbarrier

Source: Internet
Author: User

It is still a legacy issue of the last oracle migration to mysql. When I checked the data, I found that the data volume of the two databases was inconsistent. Finally, I confirmed my opinion. When I modified the code, I simplified 10 processes into one single thread. So there will be so much less data.

The reason is very simple. At that time, this function was called javasicbarrier. At that time, I didn't understand the specific meaning:

 
 
  1. If (sessionParameter. getSourceThreadNum ()> 1 ){
  2. System. out. println ("multi-thread running ");
  3. CyclicBarrier barrier = new CyclicBarrier (sessionParameter
  4. . GetSourceThreadNum () + 2 );
  5. For (int I = 0; I <= sessionParameter. getSourceThreadNum (); I ++ ){
  6. DataSyncThread dataSync = new dataSyncThread (barrier, I,
  7. SDataSource, tDataSource, sessionParameter );
  8. DataSync. start ();
  9. }
  10. Long start = System. currentTimeMillis ();
  11. Barrier. await ();
  12. Barrier. await ();
  13. Long end = System. currentTimeMillis ();

In practical applications, sometimes multiple threads need to work at the same time to accomplish the same thing, and in the process of completion, they often wait for other threads to finish a certain stage before execution, after All threads reach a certain stage, they are executed in a unified manner. Among them, barrier is equivalent to a blocking, barrier. The most important attribute of javasicbarrier is the number of participants, and the most important method is await (). When await () is called by all threads, it indicates that these threads can continue to run; otherwise, they will wait. Only when the number in CyclicBarrierint number) is reached can the 10 processes in the program be synchronized without mutual interference.

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.