helloApp
helloApp

Reputation: 459

My countdown only drops by 1 and no more, why is that?

So , i want to create a countdown for a user inputed date in a service class.Right now i have problems with the countdown.It seems no matter what it only substracts 1 second and doesn't go further.Here is the code:

the logic for the countdown

package com.example.service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class CounterService {
    LocalDateTime dateTime;

    public LocalDateTime getDateTime(){
        return dateTime;
    }

    public void setDateTime(LocalDateTime dateTime) {
        this.dateTime = dateTime;
    }

    public LocalDateTime parseDate(String eventDate) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        dateTime = LocalDateTime.parse(eventDate, formatter);
        return dateTime;
    }


    public static void main(String[] args) {

        CounterService cs = new CounterService();
        LocalDateTime a = LocalDateTime.of(2021,11,11,21,43,44);
        cs.setDateTime(a);

        Runnable r = new Counter(cs);
        new Thread(r).start();

    }
}

and the Runnable

package com.example.service;

import java.time.LocalDateTime;

public class Counter implements Runnable {
    CounterService counter;

    public Counter(CounterService cs) {
        this.counter = cs;
    }

    public void setCounter(CounterService cs) {
        this.counter = cs;
    }

    public void run() {
        LocalDateTime countFromThisDate = counter.getDateTime();
        try {
            boolean x = true;
            while (x) {
                LocalDateTime substracting = countFromThisDate.minusSeconds(1);
                Thread.sleep(1000);
                System.out.println(substracting);
            }
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}

also how will i be able to add this counter (after it works) so that it refreshes dynamically and substracts each second one by one?

Upvotes: 0

Views: 71

Answers (2)

mejariamol
mejariamol

Reputation: 78

Stating an implementation flaw here - Thread.sleep doesn't guarantee that the thread is run after the passed interval, it just makes the thread state ready (to be run by cpu) after the interval. Instead, you should use the system time to get the time elapsed since the counter start.

-- UPDATE --

A small change in your CounterService will do the work. CounterService will also keep a startTime besides the countdown datetime. The code will look something like below -

class CounterService {      

    private LocalDateTime dateTime;

    private LocalDateTime startTime;

    public CounterService(LocalDateTime dateTime, LocalDateTime startTime) {
        this.dateTime = dateTime;
        this.startTime = startTime;
    }

    public LocalDateTime getCounterTime() {
        return dateTime.minusSeconds(startTime.until(LocalDateTime.now(), ChronoUnit.SECONDS));
    }

    public static void main (String[] args) throws java.lang.Exception {
        LocalDateTime a = LocalDateTime.of(2021,11,11,21,43,44);
        LocalDateTime startTime = LocalDateTime.now();

        final CounterService counterService = new CounterService(a, startTime);

        new Thread(() -> {
            while (true) {
                System.out.println(counterService.getCounterTime());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

Upvotes: 1

Thilo
Thilo

Reputation: 262794

LocalDateTime is immutable.

countFromThisDate.minusSeconds(1) returns a new instance, it does not update the instance it is called upon. You'd need to assign the result back to somewhere (for example by calling setDateTime on your service).

This works just like as it does with String.

That service also needs to be made thread-safe (for this, you should probably move the substraction logic into the service itself, so that it can become atomic instead of get/subtract/set).

Upvotes: 0

Related Questions