简体   繁体   中英

Work manager using thread.sleep or delay doesn't work

I used Spring framework and oracle weblogic 10.3 as a container. I used workmanager for manage my thread, I already made one thread that managed by workmanager. Fortunately spring provide the delegation class for using workmanager, so I just need to put it on applicationContext.xml.

But when I put the "while" and TimeUnit for sleep the process on desired delayed time, the deployment process never finished. It seems the deployment process never jump out from while loop for finishing the deployment.

Why?, As I know using typical thread, there is no issue like this. Should I use another strategy for make it always loop and delay.

import java.util.concurrent.TimeUnit;

import org.springframework.core.task.TaskExecutor;

public class TaskExecutorSample{
    Boolean shutdown = Boolean.FALSE;
    int delay = 8000;
    TimeUnit unit = TimeUnit.SECONDS;

    private class MessageGenerator implements Runnable {
        private String message;
        public MessageGenerator(String message){
            this.message = message;
        }

        @Override
        public void run() {
            System.out.println(message);
        }
    }


    private TaskExecutor taskExecutor;
    public TaskExecutorSample(TaskExecutor taskExecutor){
        this.taskExecutor = taskExecutor;
        try {
            while (shutdown.equals(Boolean.FALSE)){
                this.printMessage();
                unit.sleep(delay);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void printMessage() {
        taskExecutor.execute(new MessageGenerator("Print this Messages"));
    }
}

Really thanks in advance. Regards,

Kahlil

Well, the thread will wait for a bit more than 2h. Did you really wait that long for the deployment to finish?

[EDIT] You're probably doing the wait in the wrong place: You should wait in the run() method of the thread, not the constructor of the class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM