You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/06/28 04:12:34 UTC

[GitHub] [pulsar] BewareMyPower commented on pull request #16256: [fix][client] Specify the init parameters value in the OpSendMsg

BewareMyPower commented on PR #16256:
URL: https://github.com/apache/pulsar/pull/16256#issuecomment-1168198165

   In addition, adding initial values to a recyclable class is dangerous. For a recyclable class, we should make constructors private and only expose some factory methods and set the value in these factory methods.
   
   The reason is that if the object was reused from the pool, the initial value should be what was set in `recycle()` method.
   
   For example,
   
   ```java
   @Data
   class RecyclableInteger {
       private static final Recycler<RecyclableInteger> RECYCLER = new Recycler<RecyclableInteger>() {
           @Override
           protected RecyclableInteger newObject(Handle<RecyclableInteger> handle) {
               return new RecyclableInteger(handle);
           }
       };
   
       private final Recycler.Handle<RecyclableInteger> handle;
       private int x = 100; // initial value: 100
   
       private RecyclableInteger(Recycler.Handle<RecyclableInteger> handle) {
           this.handle = handle;
       }
   
       public static RecyclableInteger create() {
           return RECYCLER.get(); // retrieve an object without modifying the value of `x`
       }
   
       public void recycle() {
           this.x = 0; // recycled value: 0
           handle.recycle(this);
       }
   }
   ```
   
   ```java
           RecyclableInteger i = RecyclableInteger.create();
           System.out.println(i.getX()); // 100 as we expected
           i.recycle();
           i = RecyclableInteger.create();
           System.out.println(i.getX()); // 0, which is unexpected
   ```


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

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org