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: 
You can use a ScheduledExecutorService to schedule the work to take place after certain amount of time. 

```
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
        scheduler.schedule(() -> {
            /*
             * define work to be done inside this lambda
             */
        }, diffTime, TimeUnit.MILLISECONDS);

```

Note: if you want the work to happen every Friday at midnight (therefore not just this coming Friday), you can modify the schedule command to happen at a set interval:

```
scheduler.scheduleAtFixedRate(() -> {
    /*
     * define work to be done inside this lambda
     */
    }, diffTime, Duration.ofDays(7).toMillis(), TimeUnit.MILLISECONDS);

```
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.