简体   繁体   English

使用thread.sleep或delay的工作管理器不起作用

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

I used Spring framework and oracle weblogic 10.3 as a container. 我使用Spring框架和oracle weblogic 10.3作为容器。 I used workmanager for manage my thread, I already made one thread that managed by workmanager. 我使用workmanager来管理我的线程,我已经制作了一个由workmanager管理的线程。 Fortunately spring provide the delegation class for using workmanager, so I just need to put it on applicationContext.xml. 幸运的是spring提供了使用workmanager的委托类,因此我只需要将其放在applicationContext.xml即可。

But when I put the "while" and TimeUnit for sleep the process on desired delayed time, the deployment process never finished. 但是,当我将“ while”和TimeUnit放入所需的延迟时间使进程进入睡眠状态时,部署过程从未完成。 It seems the deployment process never jump out from while loop for finishing the deployment. 似乎部署过程从不会从while循环中跳出来完成部署。

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. 好了,线程将等待超过2小时。 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. [编辑]您可能在错误的地方进行了等待:您应该在线程的run()方法中等待,而不是在类的构造函数中等待。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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