Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7497829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:14:17+00:00 2026-05-29T19:14:17+00:00

My problem is: I have a class which accepts files in a directory. Whenever

  • 0

My problem is: I have a class which accepts files in a directory. Whenever the first object arrives it starts some timer to perform a compress on all the files in a directory which arrive during the next 60 seconds.

This raises two requirements:

1) I need to be able to check if the “countdown” is already running when a new file arrives. I don’t want file2 to arrive 15 seconds after file1, then they get compressed together 45 seconds later only to have a second scheduled task fire 15 seconds after the first completed (to an empty directory).

2) Smart batching / non-constant polling. For example, if file1 arrives at time=0 and file2 arrives at time=59s, they would be compressed together. However, if file3 doesn’t arrive until time=89s, and file4 arrives at time=129s, I want to make sure the next “compress” operation is not happening at timer=120s, but rather at timer=149s (60 seconds after file3 arrived).

In other words: only one timer/countdown should ever be running. If it’s the first file since a compression, it should trigger a new one, but otherwise it should just be ignored.

I’m looking into java.util.Timer and Java.util.concurent.ScheduledExecutorService as solutions, but both seem built for having multiple processes, which I am trying to avoid.

Is there any better solution for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:14:22+00:00Added an answer on May 29, 2026 at 7:14 pm

    Here is an example code:

    private Thread countDownThread;
    
    public synchronized void onNewFile() {
        startCountDownThreadIfNotYetRunning();
    }
    
    public synchronized void resetThread() {
        if(uncompressedFilesExist()) {
            startCountDownThreadIfNotYetRunning();
        }
    }
    
    public synchronized void startCountDownThreadIfNotYetRunning() {
        if(countDownThread == null) {
            new Thread(new CompressionTask()).start();
        }
    }
    
    private class CompressionTask implements Runnable {
        @Override
        public void run() {
            try {
                Thread.sleep(60 * DateUtils.MILLIS_PER_SECOND);
                compressDirectoryContents();
                resetThread();
            }
            catch (InterruptedException e) { }
        }
    }
    
    1. When onNewFile() is called it tries to start a new countDownThread if it is not already running.

    2. This thread waits for 60 seconds and starts compression.

    3. The last step resetThread() is a bit tricky. If we simply call:

      countDownThread = null;
      

      the code would not be thread-safe. First of all change made to countDownThread might not be visible by other threads. Moreover if new file appears between compressDirectoryContents() and simple assignment, it would get lost. That’s why all operations are synchronized using the same lock.

      Because both onNewFile() and resetThread() are guarded by the same lock, it is not possible that new file appears but the count-down thread is not started.

    Note that you don’t need any fancy Timers or ScheduleExecutorService – creating a single thread every minute isn’t an overkill.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my problem, I have a class which have a object who throw
The problem: I have a class which contains a template method execute which calls
I have the following tricky problem: I have implemented a (rather complicated) class which
I have the following problem: I have the class: class Word(object): def __init__(self): self.id
I have a Project model which accepts nested attributes for Task. class Project <
I have this code which reads all the files from a directory. File textFolder
I encounters a problem where I want to have a class in which its
i am facing one problem.i have a class named ReportingService and it should be
I have a problem in calling a template class I have. I declared a
I have problem organizing my unittest based class test for family of tests. For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.