Question: I am using spring `ThreadPoolTaskExecutor` in my application. One of my methods need to be called only on Friday midnight. That method is called on server startup. So I am using the `Thread.sleep()` in the following way

```
Calendar today = Calendar.getInstance();
int dayOfWeek = today.get(Calendar.DAY_OF_WEEK);
int daysUntilNextFriday = Calendar.FRIDAY - dayOfWeek;
if(daysUntilNextFriday < 0){
    daysUntilNextFriday = daysUntilNextFriday + 7;
}       

Calendar c = Calendar.getInstance();

c.add(Calendar.DAY_OF_YEAR, daysUntilNextFriday);
c.set(Calendar.HOUR_OF_DAY, 0); 
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
Date d1 = c.getTime();

long diffTime = d1.getTime() - new Date().getTime();

Thread.sleep(diffTime);

```

In some cases it may wait for more than 1 day as well. Is there any better approach instead of using `Thread.sleep()`. I have seen in some places using `Thread.sleep()` could cause a performance issue. In my case is there any alternative to improve performance?

**Initially we scheduled nearly 50 services using ThreadPoolTaskExecutor. some of the services are not starting properly .We were unable to find the exact issue from past 1 month.So we wanted to try with executor**

Please refer this [Spring scheduler is stopping unexpectedly and starting again](https://stackoverflow.com/questions/40234549/spring-scheduler-is-stopping-unexpectedly-and-starting-again)

Answer: 
Use a [ScheduledExecutorService](http://docs.oracle.com/javase/8/docs/api/index.html?java/util/concurrent/ScheduledExecutorService.html):

```
private static final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);

```

**If you are not worried about exactness** (like caring for daylight savings shifts): 

You can approximate a fixed periodicity:

7 \* 24 \* 60 \* 60 \* 1000 = 604800000 (a week in ms)

Then you can call the executor like this:

```
Runnable task = <your task extending Runnable>    
long difftime = <time to next friday>;
long week = 7 * 24 * 60 * 60 * 1000;

executor.scheduleAtFixedRate(task, difftime, week, TimeUnit.MILLISECONDS);

```

This will cause the executor to first wait for the difftime-delay, then execute the task with the specified periodicity.

**If you *are* worried about exactness**:
Then instead call:

```
executor.schedule(task, difftime, TimeUnit.MILLISECONDS);

```

which will only execute your task once. In your task - call a method that will re-schedule the task for the next execution with a newly calculated difftime.
As advised by other users use `ThreadPoolTaskScheduler`, it is provided by Spring framework and resides in the same package `ThreadPoolTaskExecutor` does.

Another alternative is `ScheduledExecutorService`, which is provided by Core Java and has similar functionality.

Both services allow you to configure thread pool size (500 or whatever you want). But remember, if you your workers trigger at different time frames you can reuse smaller amount of threads, especially if they do not run for a long period of time.

Additionally make sure you intercept all exceptions which occur in your workers to avoid unexpected failures you observe.

There two common patterns to accomplish this:

1. Surround worker code in try / catch block:

```
public void run() {
    try {
        doRun();
    } catch (Throwable t) {
        LOG.error("exception occurred in worker", t);
    }
}

```
2. Use `UncaughtExceptionHandler` to intercept and log failures in all threads where exceptions were not intercepted:

```
public class DefaultUncaughtExceptionHandler implements UncaughtExceptionHandler {
    static {
        if (Thread.getDefaultUncaughtExceptionHandler() == null) {
            Thread.setDefaultUncaughtExceptionHandler(new DefaultUncaughtExceptionHandler());
        }
    }

    @Override
    public void uncaughtException(Thread t, Throwable e) {
        if (!(e instanceof ThreadDeath)) {
            LOG.error("uncaught exception occurred in thread " + t.getName(), e);
        }
    }
}

```