Ledger Nano X - The secure hardware wallet
SHARE
TWEET

Untitled

a guest Apr 2nd, 2020 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.company;
  2.  
  3. import java.util.concurrent.ExecutionException;
  4. import java.util.concurrent.ExecutorService;
  5. import java.util.concurrent.Executors;
  6. import java.util.concurrent.Future;
  7. import java.util.concurrent.locks.Lock;
  8. import java.util.concurrent.locks.ReentrantLock;
  9. import java.util.function.Supplier;
  10.  
  11. public class Main {
  12.  
  13.     public static abstract class AbstractAdder {
  14.         long res = 0;
  15.  
  16.         public void add(String toAdd) {
  17.             runWithSync(() -> res += 1);
  18.         }
  19.  
  20.         protected abstract void runWithSync(Supplier<?> dd);
  21.     }
  22.  
  23.     public static class SyncAdder extends AbstractAdder{
  24.  
  25.         @Override
  26.         protected void runWithSync(Supplier<?> dd) {
  27.             synchronized (this) {
  28.                 dd.get();
  29.             }
  30.         }
  31.     }
  32.  
  33.     public static class LockAdder extends AbstractAdder {
  34.  
  35.         private final Lock lock = new ReentrantLock();
  36.  
  37.         @Override
  38.         protected void runWithSync(Supplier<?> dd) {
  39.             lock.lock();
  40.             try {
  41.                 dd.get();
  42.             } finally {
  43.                 lock.unlock();
  44.             }
  45.         }
  46.     }
  47.  
  48.     public static void main(String[] args) throws ExecutionException, InterruptedException {
  49.         AbstractAdder sync = new SyncAdder();
  50.         AbstractAdder lock = new LockAdder();
  51.  
  52.         runAdder(sync, "sync");
  53.         runAdder(lock, "lock");
  54.         System.out.println();
  55.         runAdder(sync, "sync");
  56.         runAdder(lock, "lock");
  57.         System.out.println();
  58.         runAdder(sync, "sync");
  59.         runAdder(lock, "lock");
  60.         System.out.println();
  61.         runAdder(sync, "sync");
  62.         runAdder(lock, "lock");
  63.         System.out.println();
  64.         runAdder(sync, "sync");
  65.         runAdder(lock, "lock");
  66.         System.out.println();
  67.         runAdder(sync, "sync");
  68.         runAdder(lock, "lock");
  69.         System.out.println();
  70.         runAdder(sync, "sync");
  71.         runAdder(lock, "lock");
  72.     }
  73.  
  74.     private static void runAdder(AbstractAdder adder, String name) throws ExecutionException, InterruptedException {
  75.         int executors = 10;
  76.         int adds = 10000000;
  77.  
  78.         long l = System.currentTimeMillis();
  79.         ExecutorService executorService = Executors.newFixedThreadPool(executors);
  80.         Future[] futures = new Future[executors];
  81.         for (int i = 0; i < executors; i++) {
  82.             Future<Void> future = executorService.submit(() -> {
  83.                 for (int j = 0; j < adds; j++) {
  84.                     adder.add("d");
  85.                 }
  86.                 return null;
  87.             });
  88.             futures[i] = future;
  89.         }
  90.         for (Future future : futures) {
  91.             future.get();
  92.         }
  93.         executorService.shutdown();
  94.         System.out.println(name + " spent " + (System.currentTimeMillis() - l) + " and got " + adder.res);
  95.     }
  96. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top