You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by gn...@apache.org on 2018/11/07 08:41:18 UTC

[camel] 03/03: Simplify locking in GenericFileProducer

This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch sandbox/camel-3.x
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 6cffd78c7c5ce4fadb1de72a20d0f0fe53f1e70f
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Wed Nov 7 09:39:32 2018 +0100

    Simplify locking in GenericFileProducer
---
 .../org/apache/camel/component/file/GenericFileProducer.java | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java b/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
index 4b01f58..144fca6 100644
--- a/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
+++ b/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.file;
 
 import java.io.File;
+import java.util.Collections;
 import java.util.Map;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
@@ -41,7 +42,7 @@ public class GenericFileProducer<T> extends DefaultProducer {
     protected final GenericFileEndpoint<T> endpoint;
     protected GenericFileOperations<T> operations;
     // assume writing to 100 different files concurrently at most for the same file producer
-    private final Map<String, Lock> locks = LRUCacheFactory.newLRUCache(100);
+    private final Map<String, Lock> locks = Collections.synchronizedMap(LRUCacheFactory.newLRUCache(100));
 
     protected GenericFileProducer(GenericFileEndpoint<T> endpoint, GenericFileOperations<T> operations) {
         super(endpoint);
@@ -66,14 +67,7 @@ public class GenericFileProducer<T> extends DefaultProducer {
 
         // use lock for same file name to avoid concurrent writes to the same file
         // for example when you concurrently append to the same file
-        Lock lock;
-        synchronized (locks) {
-            lock = locks.get(target);
-            if (lock == null) {
-                lock = new ReentrantLock();
-                locks.put(target, lock);
-            }
-        }
+        Lock lock = locks.computeIfAbsent(target, f -> new ReentrantLock());
 
         lock.lock();
         try {