ivan1221 commented on a change in pull request #3552: NIFI-6395: Thread-safety 
bug fixed and added new flag property to han…
URL: https://github.com/apache/nifi/pull/3552#discussion_r341891829
 
 

 ##########
 File path: 
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/CountText.java
 ##########
 @@ -213,10 +221,10 @@ public void onTrigger(ProcessContext context, 
ProcessSession processSession) thr
         }
         AtomicBoolean error = new AtomicBoolean();
 
-        lineCount = 0;
-        lineNonEmptyCount = 0;
-        wordCount = 0;
-        characterCount = 0;
+        final AtomicInteger lineCount = new AtomicInteger(0);
+        final AtomicInteger lineNonEmptyCount = new AtomicInteger(0);
+        final AtomicInteger wordCount = new AtomicInteger(0);
+        final AtomicInteger characterCount = new AtomicInteger(0);
 
 Review comment:
   Hi Koji,thanks for the review!! 
   I don't think I'm very clear, I'll try to explain better why I don't use 
primitive types.
   Lambda expressions (as well as anonymous classes) in Java can only access to 
the final (or effectively final) variables of the enclosing scope.
   For example, consider the following example:
   
   ```
   void fn() {
       int myVar = 42;
       Supplier<Integer> lambdaFun = () -> myVar; // error
       myVar++;
       System.out.println(lambdaFun.get());
   }
   ```
   This doesn't compile since the incrementation of myVar prevents it from 
being effectively final.
   
   In my original code, before changing it to Atomic integer, I use a array 
since java does not have this problem when you use an array, because arrays are 
reference objects. Java can capture array reference that never changes, and use 
that non-changing reference to mutate the object. Array itself provides the 
necessary level of indirection, because Java arrays are mutable.
   
   If I declare them local within the lambda expression, cause the code block 
that calls putAllAttributes to fail since they will not be accessible.Check 
other implementations of this type in nifi, for example the ForkRecord 
processor and see that it uses atomicInteger for this type of references.
   

----------------------------------------------------------------
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]


With regards,
Apache Git Services

Reply via email to