Source:
public IMqttAsyncClient getMqttSinkClient( final String brokerURI, final String topic) throws MqttException, IOException { Tuple<List<IMqttAsyncClient>, CountDownLatch> tuple = brokerPublisherMap.get(brokerURI); if (tuple == null) { final CountDownLatch latch = new CountDownLatch(0); final List<IMqttAsyncClient> publisherClientList = new ArrayList<>(); if (brokerPublisherMap.putIfAbsent(brokerURI, new Tuple<>(publisherClientList, latch)) == null) { for (int i = 0; i < mqttSinkClientNumPerBroker; i++) { createSinkClient(brokerURI, publisherClientList); } final HashMap<String, IMqttAsyncClient> myTopicPublisherMap = new HashMap<>(); topicPublisherMap.put(brokerURI, myTopicPublisherMap); latch.countDown(); } } tuple = brokerPublisherMap.get(brokerURI); final CountDownLatch latch = tuple.getValue(); try { latch.await(); } catch (InterruptedException e) { e.printStackTrace(); } final List<IMqttAsyncClient> publishClientList = tuple.getKey(); final Map<String, IMqttAsyncClient> myTopicPublisherMap = topicPublisherMap.get(brokerURI); if (myTopicPublisherMap.containsKey(topic)) { final IMqttAsyncClient client = myTopicPublisherMap.get(topic); publisherSinkNumMap.get(client).getAndIncrement(); return client; } else { int minSinkNum = Integer.MAX_VALUE; IMqttAsyncClient client = null; for (final IMqttAsyncClient mqttAsyncClient: publishClientList) { if (minSinkNum > publisherSinkNumMap.get(mqttAsyncClient).get()) { minSinkNum = publisherSinkNumMap.get(mqttAsyncClient).get(); client = mqttAsyncClient; } } publisherSinkNumMap.get(client).getAndIncrement(); myTopicPublisherMap.put(topic, client); return client; } }

Target:
public IMqttAsyncClient getMqttSinkClient( final String brokerURI, final String topic) throws MqttException, IOException { Tuple<List<IMqttAsyncClient>, CountDownLatch> tuple = brokerPublisherMap.get(brokerURI); if (tuple == null) { final CountDownLatch latch = new CountDownLatch(1); final List<IMqttAsyncClient> publisherClientList = new ArrayList<>(); if (brokerPublisherMap.putIfAbsent(brokerURI, new Tuple<>(publisherClientList, latch)) == null) { for (int i = 0; i < mqttSinkClientNumPerBroker; i++) { createSinkClient(brokerURI, publisherClientList); } final HashMap<String, IMqttAsyncClient> myTopicPublisherMap = new HashMap<>(); topicPublisherMap.put(brokerURI, myTopicPublisherMap); latch.countDown(); } } tuple = brokerPublisherMap.get(brokerURI); final CountDownLatch latch = tuple.getValue(); try { latch.await(); } catch (InterruptedException e) { e.printStackTrace(); } final List<IMqttAsyncClient> publishClientList = tuple.getKey(); final Map<String, IMqttAsyncClient> myTopicPublisherMap = topicPublisherMap.get(brokerURI); if (myTopicPublisherMap.containsKey(topic)) { final IMqttAsyncClient client = myTopicPublisherMap.get(topic); publisherSinkNumMap.get(client).getAndIncrement(); return client; } else { int minSinkNum = Integer.MAX_VALUE; IMqttAsyncClient client = null; for (final IMqttAsyncClient mqttAsyncClient: publishClientList) { if (minSinkNum > publisherSinkNumMap.get(mqttAsyncClient).get()) { minSinkNum = publisherSinkNumMap.get(mqttAsyncClient).get(); client = mqttAsyncClient; } } publisherSinkNumMap.get(client).getAndIncrement(); myTopicPublisherMap.put(topic, client); return client; } }