Shawyeok commented on issue #18269:
URL: https://github.com/apache/pulsar/issues/18269#issuecomment-1297145269

   Update:
   > `metadata-store-zk-session-watcher` thread can always acquire lock (I 
don't understand why, a jvm bug?)
   
   `synchronized` lock is unfair, a thread has big chance acquire lock if it 
just release it, see example below:
   ```java
   class Scratch {
   
       private static final CountDownLatch latch = new CountDownLatch(1);
   
       public static void main(String[] args) throws InterruptedException {
           ScheduledExecutorService scheduler = 
Executors.newSingleThreadScheduledExecutor();
           Scratch s = new Scratch();
           scheduler.scheduleAtFixedRate(s::checkState, 2, 2, TimeUnit.SECONDS);
   
           latch.await();
           while (true) {
               s.process();
           }
       }
   
       private void process() {
           checkState();
       }
   
       private synchronized void checkState() {
           System.out.println(Thread.currentThread().getName() + " checking 
state");
           if (latch.getCount() > 0) {
               latch.countDown();
           }
           CompletableFuture<Void> future = new CompletableFuture<>();
           try {
               future.get(2, TimeUnit.SECONDS);
           } catch (TimeoutException ignore) {
   
           } catch (Exception e) {
               e.printStackTrace();
           }
           System.out.println(Thread.currentThread().getName() + " finished");
       }
   }
   ```
   Output:
   ```
   pool-1-thread-1 checking state
   pool-1-thread-1 finished
   main checking state
   main finished
   main checking state
   main finished
   main checking state
   main finished
   main checking state
   main finished
   main checking state
   main finished
   main checking state
   main finished
   main checking state
   main finished
   main checking state
   main finished
   main checking state
   main finished
   main checking state
   main finished
   main checking state
   main finished
   main checking state
   main finished
   main checking state
   main finished
   main checking state
   main finished
   main checking state
   main finished
   main checking state
   main finished
   main checking state
   main finished
   main checking state
   main finished
   main checking state
   main finished
   main checking state
   main finished
   ...
   ```


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to