How to call multiple functions which recur at regular intervals and takes long time to complete in Java?

Viewed 41

I have a requirement to run a Java program with the following functionalities.

  1. There should be a function (Say fetchTimeLimits()) that fetches two values of time. Time limit A and Time limit B. Note that both these time limits are future time values of the type Instant(). The fetching of these values should happen every 5 seconds.
  2. There should be a function (Say functionA()) that utilizes time limit A. function A runs a while loop which sends out a calculated value until Time limit A is over. There is a sleep time of 1 second within the while loop.
  3. There should be a function (Say functionB()) that utilizes time limit B. function B runs a while loop which sends out a calculated value until Time limit B is over. There is a sleep time of 1 second within while loop.

To implement this, I added a timer task with an interval of 5 seconds. In the timer task, fetchTimeLimits() function is called. Also, Two separate threads are started for functionA() and functinB(), if they are null or not alive. This is because, functionA() and functionB() would take hours to complete execution.

However, the CPU usage and RAM usage of the Java program is considerably high and I can observe that the CPU usage graph shows spikes.

package com.test;

import java.time.Instant;
import java.util.Timer;
import java.util.TimerTask;

public class mainClass {

    static Thread functionAThread = null;
    static Thread functionBThread = null;
    static volatile Instant timeA = null;
    static volatile Instant timeB = null;
    static Timer timer = new Timer();

    public static void main(String[] args) {
        startTimer();
    }

    private static void startTimer() {
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                fetchTimeLimits();
                if (functionAThread == null || !functionAThread.isAlive()) {
                    functionAThread = new Thread(() -> {
                        functionA();
                    });
                    functionAThread.start();
                }
                if (functionBThread == null || !functionBThread.isAlive()) {
                    functionBThread = new Thread(() -> {
                        functionB();
                    });
                    functionBThread.start();
                }
            }
        }, 0, 5000);
    }

    private static void fetchTimeLimits() {
        // Fetch values of timeA and timeB
    }

    private static void functionA() {
        while (timeA != null && timeA.compareTo(Instant.now()) > 0) {
            try {
                Thread.sleep(1000);
                // Calculate a value and send it.
            } catch (InterruptedException e) {
            }
        }
    }

    private static void functionB() {
        while (timeB != null && timeB.compareTo(Instant.now()) > 0) {
            try {
                Thread.sleep(1000);
                // Calculate a value and send it.
            } catch (InterruptedException e) {
            }
        }
    }
}

Is there a better way to handle the requirement?

0 Answers
Related