0

I have a master thread which has its own child threads. Each thread is given a process to be performed and that particular thread must sleep after its work is done until the next frequency of thread awakening.

I have implemented the whole section of code, but not able to make the thread sleep as I'm not aware of is the job of that thread is done or not, in order to make it sleep until the next frequency time.

  1. Create Maste Thread
  2. Create child threads for each processes from the DB.
  3. For Each Child Thread Process, it checks its runtime and next runtime to perform the job.
  4. Make the thread sleep after the work is done until the next runtime.
  5. Go to Step 3.

I'm stuck at 4, to check if the threads have done its job or not. ( here the child thread doesn't say, if its work is done or not, we have it make it sleep after work is done)

Can anyone help, how it can be done?

private static ScheduledExecutorService stp = Executors.newScheduledThreadPool(11);

@Scheduled(fixedDelayString = "${con.fixedDelay}")
public void processServices() {

    try {

        logger.debug("Entering processServices in MasterProcessThread ");

        // Getting process details and schedule
        List<ProcessDomain> activeProcessList = processDomainDao.getActiveProcessDomainSchedule();

        logger.info("getDomainData for flag is Active :: " + activeProcessList.size());

        // Iterating over active processList to create child threads
        for (ProcessDomain activeProcessDomain : activeProcessList) {

            Timestamp nextRunTS = activeProcessDomain.getProcessSchedule().getProcessNextRunScheduleTime();

            Long nextRunTime = (nextRunTS != null ? nextRunTS.getTime() : null);
            Long startTS = activeProcessDomain.getProcessSchedule().getProcessScheduleTime().getTime();
            Long currentTS = System.currentTimeMillis();
            boolean isPreviousRunSucc = (activeProcessDomain.getProcessRunHistory().getPreviousSuccFlag() != null
                    && activeProcessDomain.getProcessRunHistory().getPreviousSuccFlag().equals("Y")) ? true : false;
            Future future = null;

            // Initializing the child threads and start processing
            if (((nextRunTime == null && currentTS >= startTS) || (nextRunTime != null && currentTS >= nextRunTime))
                    && isPreviousRunSucc) {
                Callable task = beanServiceThreadFactory.getBeanThread(activeProcessDomain.getProcessName(),
                        activeProcessDomain);
                future = stp.submit(task);
                if (future.isDone()){
                    stp.shutdown();
                }
            }
        }
Mani
  • 87
  • 7

0 Answers0