in Education by
I want to store in a queue, datastructure does not matter, only the elements that I have inserted within say last 5 minutes from current time. Anything older should get removed - so that any time I get the size of the queue it will give count of the objects inserted in last 5 minutes. Basically all I have to know is how many times my app has made a http call to a sever in last 5 minutes before making the next call. If anyone knows of some existing library that may have this implementation please share. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You can use a Priority Queue with timestamps as your keys. So that when you call Peek() you always get the oldest timestamp still in the queue. Then each time you go to query for the number of items inside your window size: you cleanup the items outside your window and return the number of items still in the Priority queue. For example: public class CountInWindow { /** * Adding a main just for testing * @param args * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { System.out.println("test started"); CountInWindow test = new CountInWindow(5000); //5 seconds for testing test.debug = true; test.insertTimeStamp(System.currentTimeMillis()); Thread.sleep(100);//sleep test.insertTimeStamp(System.currentTimeMillis()); Thread.sleep(100);//sleep test.insertTimeStamp(System.currentTimeMillis()); Thread.sleep(100);//sleep test.insertTimeStamp(System.currentTimeMillis()); Thread.sleep(5040);//sleep 5 secs test.insertTimeStamp(System.currentTimeMillis()); Thread.sleep(100);//sleep test.insertTimeStamp(System.currentTimeMillis()); System.out.println(test.getWindowCount()); //Should be 2 not 6. System.out.println("test done"); } java.util.PriorityQueue window; public static final long FIVE_MINS_IN_MS = 300000l; public final long WINDOW_SIZE; public boolean debug = false; //Constructor which defaults to 5mins public CountInWindow(){ WINDOW_SIZE = FIVE_MINS_IN_MS; window = new java.util.PriorityQueue(); } //Constructor for any size window public CountInWindow(long windowSize){ WINDOW_SIZE = windowSize; window = new java.util.PriorityQueue(); } /** * Add a new timestamp to the window's queue * @param ts */ public void insertTimeStamp(long ts){ window.add(ts); } /** * Clean up items outside the window size and then return the count of times still in the window. * @return A count of timestamps still inside the 5 mins window. */ public int getWindowCount(){ long currTime = System.currentTimeMillis(); //Clean out old Timestamps while((currTime - window.peek().longValue()) > WINDOW_SIZE){ long drop = window.remove().longValue(); if(debug)System.out.println("dropping item:" + drop); } return window.size(); } }

Related questions

0 votes
    For the second period elements the correct increasing order of first io0nization enthalpy is: A. Li...
asked Jan 2, 2022 in Education by JackTerrance
0 votes
    I am investigating the design of a work queue processor where the QueueProcessor retrieves a Command Pattern object ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I am building a basic RSS reader app that, which the 'story' is clicked, the information for it is displayed in a modal ... .log('Does Not Work!'); Or another way: - overscroll...
asked Jul 30, 2022 in Education by JackTerrance
0 votes
    Which algorithm chooses the page that has not been used for the longest period of time whenever the page ... recently used algorithm d) counting based page replacement algorithm...
asked Oct 27, 2022 in Education by JackTerrance
0 votes
    Which of these method of Thread class is used to Suspend a thread for a period of time? (a) sleep() ... portion Multithreading of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    (d) Which animation changes a shape into another over the period of time? (i) Shape Tween (ii) Motion Tween ... (iv) Tween Motion Select the correct answer from above options...
asked Dec 1, 2021 in Education by JackTerrance
0 votes
    Which of these methods of a Thread class is used to suspend a thread for a period of time? (a) ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    The relation between frequency and time period is? (a) f=1/T (b) f=T (c) f=1/T^2 (d) f=1/ ... theory Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 20, 2021 in Education by JackTerrance
0 votes
    Find the instantaneous value of the sine value at 90⁰ point having an amplitude 10V and time period 360⁰. (a) 5 ... for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 19, 2021 in Education by JackTerrance
+1 vote
    are used for analyzing trend over a period of time...
asked Oct 11, 2020 in Education by anonymous
0 votes
    I am trying to use celery to manage tasks. The problem i am into now, that i have many minor ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    You can time ________ expressions by wrapping them in curly braces within the call to system.time(). (a) ... Analysis of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
...