androidkaifa1 commented on issue #2170:
URL: https://github.com/apache/rocketmq/issues/2170#issuecomment-662198516


   @francisoliverlee 
   Thanks for your answer, I know what you mean. But `public synchronized void 
registerBrokerAll()` This code just makes the following code
   
   ```java
    // 启动了一个定时调度的任务,让他去给 NameServer 进行注册
           // 默认 30s 发送一次
           this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
   
               @Override
               public void run() {
                   try {
                       // 将自己注册到NameServer
                       BrokerController.this.
                               registerBrokerAll(true, false, 
brokerConfig.isForceRegister());
                   } catch (Throwable e) {
                       log.error("registerBrokerAll Exception", e);
                   }
               }
           }, 1000 * 10, Math.max(10000, 
Math.min(brokerConfig.getRegisterNameServerPeriod(), 60000)), 
TimeUnit.MILLISECONDS);
   
   ```
   Thread safety in the thread pool. In other words, he only guarantees the 
thread safety of different batches of timing tasks. However, it is still not 
possible to guarantee the thread safety of timed tasks in the same batch. For 
example: send a heartbeat a certain time. Broker needs to send heartbeats to 10 
NameServers. `public List<RegisterBrokerResult> registerBrokerAll`The method is 
to open 10 threads to send heartbeats, if the results are returned at the same 
time, modify `registerBrokerResultList ArrayList` There are still concurrency 
security issues.
   
   `public synchronized void registerBrokerAll()` This code just guarantees, 
for example, time 0 and time 30. The heartbeats they send have no concurrent 
security issues.
   
   The logic in MQ is similar to the following code logic.
   
   ```java
   package com.example.test.arraylistrocketmq;
   
   import com.google.common.collect.Lists;
   import org.junit.Test;
   
   import java.util.ArrayList;
   import java.util.List;
   import java.util.concurrent.ArrayBlockingQueue;
   import java.util.concurrent.CountDownLatch;
   import java.util.concurrent.TimeUnit;
   import java.util.concurrent.atomic.AtomicInteger;
   
   /**
    * @author Chen
    * @version 1.0
    * @date 2020/7/20 9:20
    * @description:
    */
   public class TestChen2 {
   
   
       @Test
       public void test() throws InterruptedException {
           for (int i = 0; i < 100; i++) {
               int finalI = i;
               Thread thread = new Thread(new Runnable() {
                   @Override
                   public void run() {
                       deal(finalI);
                   }
               });
               thread.start();
   
               thread.join();
           }
       }
   
       public synchronized void deal(int index) {
           // 初始化一个List,存放每个NameServer注册结果的
           final List<RegisterBrokerResult> registerBrokerResultList =
                   Lists.newArrayList();
           // 获取 NameServer 地址列表
           List<String> nameServerAddressList = new ArrayList<>();
           for (int j = 0; j < 10; j++) {
               nameServerAddressList.add("192.168.0." + j);
           }
           AtomicInteger atomicInteger = new AtomicInteger(0);
           // 执行其他逻辑
           final CountDownLatch countDownLatch = new 
CountDownLatch(nameServerAddressList.size());
   
           BrokerFixedThreadPoolExecutor brokerOuterExecutor = new 
BrokerFixedThreadPoolExecutor(4, 10, 1, TimeUnit.MINUTES,
                   new ArrayBlockingQueue<Runnable>(32), new 
ThreadFactoryImpl("brokerOutApi_thread_", true));
   
           // 遍历NameServer 地址列表,使用线程池去注册
           for (final String namesrvAddr : nameServerAddressList) {
               brokerOuterExecutor.execute(new Runnable() {
                   @Override
                   public void run() {
                       try {
                           // 调用 registerBroker 真正执行注册
                           RegisterBrokerResult result = new 
RegisterBrokerResult();
                           result.setHaServerAddr("chen" + 
atomicInteger.getAndIncrement());
                           Thread.sleep(100);
                           if (result != null) {
                               // 注册成功结果放到一个List里去
                               registerBrokerResultList.add(result);
                           }
   
                       } catch (Exception e) {
                           System.out.println("-----------wei---------------> " 
+ index);
                           System.out.println(e);
                           System.out.println("-----------wei---------------> " 
+ index);
                       } finally {
                           // 注册完,执行 countDownLatch.countDown(); 同步计数器 -1
                           countDownLatch.countDown();
                       }
                   }
               });
           }
   
           try {
               // 等待所有 NameServer 都注册完,才返回注册结果
               countDownLatch.await(1000000, TimeUnit.MILLISECONDS);
           } catch (InterruptedException e) {
           }
   
           System.out.println(registerBrokerResultList.size());
   //        System.out.println(registerBrokerResultList);
           
System.out.println("-----------chen---------------------------------------> ");
       }
   }
   
   ```
   
   But the above code cannot guarantee thread safety.
   
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to