|
| 1 | +package com.interview.multithreaded; |
| 2 | + |
| 3 | +import java.util.Random; |
| 4 | +import java.util.Timer; |
| 5 | +import java.util.TimerTask; |
| 6 | +import java.util.concurrent.CountDownLatch; |
| 7 | +import java.util.concurrent.ExecutorService; |
| 8 | +import java.util.concurrent.Executors; |
| 9 | +import java.util.concurrent.atomic.AtomicLongArray; |
| 10 | + |
| 11 | +/** |
| 12 | + * Date 03/03/2015 |
| 13 | + * @author tusroy |
| 14 | + * |
| 15 | + * Develop a software to count number of events in last 5 mins. You have to support two apis |
| 16 | + * 1) addEvent() -> It means increment event by 1 |
| 17 | + * 2) getTotalEvents() -> Return total number of events in last 5 mins |
| 18 | + * |
| 19 | + * Program should support millions of events every minute and should also provide multi-threading support |
| 20 | + * |
| 21 | + * This class might not have 100% accuracy as far as events in last 5 mins are concerned. |
| 22 | + * Since we are using circular queue last second information may not be very accurate. |
| 23 | + * |
| 24 | + * Solution: |
| 25 | + * Keep atomiclong of 300 in array. PositionUpdater updates position every second. |
| 26 | + */ |
| 27 | +public class RealTimeCounter { |
| 28 | + |
| 29 | + private final static int GRANULARITY = 300; |
| 30 | + private AtomicLongArray counter = new AtomicLongArray(GRANULARITY); |
| 31 | + private volatile int pos = 0; |
| 32 | + |
| 33 | + private RealTimeCounter(){ |
| 34 | + PositionUpdater positionUpdater = new PositionUpdater(this); |
| 35 | + positionUpdater.start(); |
| 36 | + } |
| 37 | + |
| 38 | + private static volatile RealTimeCounter INSTANCE; |
| 39 | + |
| 40 | + public static RealTimeCounter getInstance(){ |
| 41 | + if(INSTANCE == null){ |
| 42 | + synchronized (RealTimeCounter.class) { |
| 43 | + if(INSTANCE == null){ |
| 44 | + INSTANCE = new RealTimeCounter(); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + return INSTANCE; |
| 49 | + } |
| 50 | + |
| 51 | + public long getTotalEvents(){ |
| 52 | + int total = 0; |
| 53 | + for(int i=0; i < GRANULARITY; i++){ |
| 54 | + total += counter.get(i); |
| 55 | + } |
| 56 | + return total; |
| 57 | + } |
| 58 | + |
| 59 | + public void addEvent(){ |
| 60 | + counter.getAndIncrement(pos); |
| 61 | + } |
| 62 | + |
| 63 | + void incrementPosition(){ |
| 64 | + pos = (pos + 1)%GRANULARITY; |
| 65 | + } |
| 66 | + |
| 67 | + public static void main(String args[]){ |
| 68 | + ExecutorService executor = Executors.newFixedThreadPool(10); |
| 69 | + RealTimeCounter realTimeCounter = new RealTimeCounter(); |
| 70 | + final Random random = new Random(); |
| 71 | + final int TOTAL_EVENTS = 10000; |
| 72 | + CountDownLatch countDownLatch = new CountDownLatch(TOTAL_EVENTS); |
| 73 | + for(int i=0; i < TOTAL_EVENTS; i++){ |
| 74 | + executor.execute(() -> { |
| 75 | + realTimeCounter.addEvent(); |
| 76 | + try { |
| 77 | + Thread.sleep(random.nextInt(10)); |
| 78 | + } catch (Exception e) { |
| 79 | + e.printStackTrace(); |
| 80 | + } |
| 81 | + countDownLatch.countDown(); |
| 82 | + } |
| 83 | + ); |
| 84 | + } |
| 85 | + try{ |
| 86 | + countDownLatch.await(); |
| 87 | + }catch(Exception e){ |
| 88 | + |
| 89 | + } |
| 90 | + System.out.println(realTimeCounter.getTotalEvents()); |
| 91 | + executor.shutdownNow(); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +class PositionUpdater extends TimerTask{ |
| 96 | + |
| 97 | + private final RealTimeCounter realTimeCounter; |
| 98 | + private final Timer timer = new Timer(true); |
| 99 | + private static final int DELAY = 1000; |
| 100 | + PositionUpdater(RealTimeCounter realTimeCounter) { |
| 101 | + this.realTimeCounter = realTimeCounter; |
| 102 | + } |
| 103 | + |
| 104 | + public void start(){ |
| 105 | + timer.schedule(this, DELAY); |
| 106 | + } |
| 107 | + @Override |
| 108 | + public void run() { |
| 109 | + realTimeCounter.incrementPosition(); |
| 110 | + } |
| 111 | +} |
0 commit comments