Advertisement
Jaydeep999997

Token Bucket Filter - Background Thread

Oct 21st, 2024
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | Source Code | 0 0
  1. import java.util.HashSet;
  2. import java.util.Set;
  3.  
  4. class Demonstrator {
  5.   public static void main(String[] args) throws Exception {
  6.     TokenBucketFilter tokenBucketFiller = TokenBucketFillerFactory.create(1);
  7.     Set<Thread> threads = new HashSet<Thread>();
  8.  
  9.     for (int i = 0; i < 10; i++) {
  10.       Thread thread =
  11.           new Thread(
  12.               new Runnable() {
  13.                 @Override
  14.                 public void run() {
  15.                   try {
  16.                     tokenBucketFiller.consumeToken();
  17.                   } catch (InterruptedException ie) {
  18.  
  19.                   }
  20.                 }
  21.               });
  22.       thread.setName("Thread " + i);
  23.       threads.add(thread);
  24.     }
  25.  
  26.     for (Thread thread : threads) {
  27.       thread.start();
  28.     }
  29.  
  30.     for (Thread thread : threads) {
  31.       thread.join();
  32.     }
  33.   }
  34. }
  35.  
  36. public abstract class TokenBucketFilter {
  37.   public void consumeToken() throws InterruptedException {}
  38. }
  39.  
  40. class TokenBucketFillerFactory {
  41.   private TokenBucketFillerFactory() {}
  42.  
  43.   public static TokenBucketFilter create(int maxCapacity) {
  44.     MultiThreadedTokenBucketFiller tokenBucketFiller =
  45.         new MultiThreadedTokenBucketFiller(maxCapacity);
  46.     tokenBucketFiller.initializeProducer();
  47.  
  48.     return tokenBucketFiller;
  49.   }
  50.  
  51.   private static class MultiThreadedTokenBucketFiller extends TokenBucketFilter {
  52.     private int maxCapacity;
  53.     private int consumableTokens = 0;
  54.  
  55.     public MultiThreadedTokenBucketFiller(int maxCapacity) {
  56.       this.maxCapacity = maxCapacity;
  57.     }
  58.  
  59.     public void initializeProducer() {
  60.       Thread producer = new Thread(() -> {
  61.         produceTokens();
  62.       });
  63.  
  64.       producer.setDaemon(true);
  65.       producer.start();
  66.     }
  67.    
  68.     private void produceTokens() {
  69.       while(true) {
  70.         synchronized(this) {
  71.           if(consumableTokens < maxCapacity) {
  72.             consumableTokens++;
  73.           }
  74.           this.notify();
  75.         }
  76.        
  77.         try {
  78.           Thread.sleep(1000);
  79.         } catch (InterruptedException ie) {
  80.          
  81.         }
  82.       }
  83.     }
  84.  
  85.     public void consumeToken() throws InterruptedException {
  86.       synchronized (this) {
  87.         while (consumableTokens == 0) {
  88.           this.wait();
  89.         }
  90.         consumableTokens--;
  91.       }
  92.  
  93.       System.out.println(
  94.           "Granting "
  95.               + Thread.currentThread().getName()
  96.               + " a token at time "
  97.               + (System.currentTimeMillis() / 1000));
  98.     }
  99.   }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement