Advertisement
Jaydeep999997

Deferred Callback - Reentrant Lock

Oct 21st, 2024
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | Source Code | 0 0
  1. import java.util.*;
  2. import java.util.concurrent.locks.ReentrantLock;
  3. import java.util.concurrent.locks.Condition;
  4. import java.util.concurrent.TimeUnit;
  5.  
  6. class Demonstration {
  7.   public static void main(String[] args) throws Exception {
  8.     DeferredCallback deferredCallback = new DeferredCallback();
  9.     Set<Thread> allThreads = new HashSet<Thread>();
  10.  
  11.     Thread service =
  12.         new Thread(
  13.             new Runnable() {
  14.  
  15.               @Override
  16.               public void run() {
  17.                 try {
  18.                   deferredCallback.executeCallback();
  19.                 } catch (InterruptedException ie) {
  20.  
  21.                 }
  22.               }
  23.             });
  24.  
  25.     service.start();
  26.  
  27.     for (int i = 0; i < 3; i++) {
  28.       final int index = i;
  29.       Thread thread =
  30.           new Thread(
  31.               () -> {
  32.                 Callback callback = new Callback(3000 - index * 1000, Thread.currentThread().getName());
  33.                 deferredCallback.scheduleCallback(callback);
  34.               });
  35.       thread.setName("Thread_" + i);
  36.       thread.start();
  37.       allThreads.add(thread);
  38.       Thread.sleep(100);
  39.     }
  40.  
  41.     for (Thread thread : allThreads) {
  42.       thread.join();
  43.     }
  44.   }
  45. }
  46.  
  47. class DeferredCallback {
  48.   private final PriorityQueue<Callback> callbackQueue =
  49.       new PriorityQueue<Callback>(
  50.           new Comparator<Callback>() {
  51.  
  52.             public int compare(Callback c1, Callback c2) {
  53.               return (int) (c1.executeAt - c2.executeAt);
  54.             }
  55.           });
  56.   private final ReentrantLock lock = new ReentrantLock();
  57.   private final Condition condition = lock.newCondition();
  58.  
  59.   public DeferredCallback() {}
  60.  
  61.   public void executeCallback() throws InterruptedException {
  62.     while (true) {
  63.       lock.lock();
  64.       while (callbackQueue.isEmpty()) {
  65.         condition.await();
  66.       }
  67.  
  68.       // Now queue has something
  69.       while (!callbackQueue.isEmpty()) {
  70.         long latestScheduleTime = computeLatestScheduleTime();
  71.  
  72.         if (latestScheduleTime <= 0) {
  73.           break;
  74.         }
  75.  
  76.         condition.await(latestScheduleTime, TimeUnit.MILLISECONDS);
  77.       }
  78.  
  79.       Callback callback = callbackQueue.poll();
  80.       System.out.println(
  81.           "Executed at: "
  82.               + System.currentTimeMillis()
  83.               + " Requested at: "
  84.               + callback.executeAt
  85.               + " Message: "
  86.               + callback.message);
  87.       lock.unlock();
  88.     }
  89.   }
  90.  
  91.   private long computeLatestScheduleTime() {
  92.     return (callbackQueue.peek().executeAt - System.currentTimeMillis());
  93.   }
  94.  
  95.   public void scheduleCallback(Callback callback) {
  96.     lock.lock();
  97.     callbackQueue.add(callback);
  98.     condition.signal();
  99.     lock.unlock();
  100.   }
  101. }
  102.  
  103. class Callback {
  104.   final long executeAt;
  105.   final String message;
  106.  
  107.   Callback(long executeAfterMillis, String message) {
  108.     this.executeAt = System.currentTimeMillis() + executeAfterMillis;
  109.     this.message = message;
  110.   }
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement