private void submitBatch(boolean force) { if (currentBatch.isEmpty()) { return; } if (isSubmitBatch(force)) { // Synchronized block is to ensure only one of the writers will actually write the batch. synchronized (currentBatch) { // If two threads pass the if condition and then one of them actually writes, // other will do the same since they were blocked sync block. this ensures that // others will not post it even if multiple threads were to wait at sync block at same time // https://en.wikipedia.org/wiki/Double-checked_locking if (isSubmitBatch(force)) { logger.debug("Submitting a batch of " + currentBatch.size() + " requests to GA"); httpClient.postBatch(new HttpBatchRequest().setUrl(config.getBatchUrl()).setRequests(currentBatch)); currentBatch.clear(); } } } }
private void submitBatch(boolean force) { if (currentBatch.isEmpty()) { return; } if (isSubmitBatch(force)) { // Synchronized block is to ensure only one of the writers will actually write the batch. synchronized (currentBatch) { // If two threads pass the if condition and then one of them actually writes, // other will do the same since they were blocked sync block. this ensures that // others will not post it even if multiple threads were to wait at sync block at same time // https://en.wikipedia.org/wiki/Double-checked_locking if (isSubmitBatch(force)) { logger.debug("Submitting a batch of " + currentBatch.size() + " requests to GA"); httpClient.postBatch(new HttpBatchRequest().setUrl(config.getBatchUrl()).setRequests(currentBatch)); currentBatch.clear(); } } } }