I am using Java as my programming interface. Here is my work flow. I have a producer who has a file name with path and adds it to queue. And then consumer who consumes it. After dequeue I add it to arraylist and then check if it has reached a count of x. If yes then I will process it. So far so good. But the problem is when I have a single file sitting in this list. It doesn't get processed till filelist has reached size x. I wanted to have some kind of timer to elapse and after y mins, I wanted to process the file list if it is not empty. How do I achieve this? I know about timer task, scheduled executor but it runs a thread after scheduled time which is not my case. I can provide more details if you need. Please let me know if my design is wrong and thanks for the help.
Asked
Active
Viewed 383 times
0
-
If you are down voting the question, please quote a reason. I am really interested to know since not everyone knows everything. – AnswerSeeker Aug 13 '17 at 03:01
1 Answers
1
Every time you try to dequeue check a timestamp that you set when you consumed the last file. If that timestamp is bigger than your threshold time and you have n-files in the queue (where n < x) - consume the files even though they haven't reached x.
pseudo-code:
timestamp <= now
every k milliseconds:
check the number of files n in queue:
if n >= x:
consume the files
update timestamp (to "now")
else if (now - timestamp > time-threshold):
consume the files
update timestamp (to "now")

Nir Alfasi
- 53,191
- 11
- 86
- 129
-
I will try that. Is there a solution in java that is similar to VC++ event where OnTimer() is fired – AnswerSeeker Aug 13 '17 at 03:05
-
@AnswerSeeker what's the event ? it sounds to me like you want to sleep for 1/2/3 seconds and check the queue in a loop - you don't need threads either - the main thread can do this! – Nir Alfasi Aug 13 '17 at 03:07
-
What I wanted to do process all the files in queue if a fixed time is elapsed or if queue is full. VC++ event i read somewhere but I might be wrong. Please ignore that – AnswerSeeker Aug 13 '17 at 03:11
-
I think that the approach above ^^^ is easy to implement. Further, if you want the queue to be processed immediately (without sleep) you can wrap the queue with your home-made class that checks the size upon insert and if the capacity has been reached - trigger a "consume" – Nir Alfasi Aug 13 '17 at 03:39
-
I tried this approach and doesn't seem to fit. I am using LinkedBlockingQueue and when i call take() it seems to wait when queue is empty. The dequeue is adding to a List
and it remains unprocessed since blocking queue take() just waits. My initial approach was to dequeue and add it to list. Once list reached the max capacity the file list is processed. Only thing missing is a timer which after expiry processes the file list. – AnswerSeeker Aug 15 '17 at 05:41 -
1I have created a ScheduledExecutorService that runs every minute and calls the processData() my custom function to process the file list. I have Synchronized the file list to make it thread safe. Thank you very much for your help. I still have accepted your answer since that approach works if you are not using blocking queue. – AnswerSeeker Aug 15 '17 at 06:40
-
@AnswerSeeker if you don't want to block, instead of `take()` you can use [`poll`](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#poll--) – Nir Alfasi Aug 15 '17 at 15:09