1

我正在使用作业队列库,您可以在其中定义Jobs 并将它们发布到JobManager. 有JobManager一个ThreadGroupwith worker 将Jobs 从队列中拉出来并调用Job.onRun()

线程的数量是通过给出最大和最小线程数量、负载因子(一个工作线程在创建一个新的之前可以有多少未完成的作业)和任何高于最小值的线程的空闲超时来设置的。因此,如果一个工作线程run()没有新的工作,它就会终止,并且达到它的空闲超时。

在某些Job情况下,我使用仅公开异步 API 的库。在 onRun() 中使用这些安全吗?

例如,如果我想发出一个网络请求,该请求接受一个为我提供响应的回调,回调将被调用:

@Override
public void onRun() throws Throwable {
    client.doRequest(request, new Callback() {
        @Override
        public void onRequestComplete(Response response) {
            //is this guaranteed to get called?
        }
    });
    //since we now return from onRun() will onRequestComplete() get called?
}
4

3 回答 3

4

是的,只要进程仍然存在,您的回调就会运行。

如果你想看看我创建了一个程序来模拟它。它有三个类,只需将它们放在任何一个包中并运行,您就会看到自己。这描绘了即使您的工作线程完成回调仍然来。我希望它有帮助:)

1级

public class Client {
    private static long startTime = System.currentTimeMillis();

    public static void main(String[] args) {
        System.out.println("Time elapsed in ms:" + (System.currentTimeMillis() - startTime));
        Client client = new Client();
        client.executeJob();
        try {
            System.out.println("Main thread sleeping");
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Main thread finishing");
    }

    public void executeJob() {
        new Thread() {
            @Override
            public void run() {
                Job job = new Job();
                System.out.println("Calling Job with Asyc - Time elapsed in ms:" 
                        + (System.currentTimeMillis() - startTime));
                job.doRequest("Calling Async", new CallBack() {

                    @Override
                    public void onRequestComplete(String response) {
                        System.out.println("Got my callback eventhough job that started the Async is over - Time elapsed in ms:" 
                                + (System.currentTimeMillis() - startTime));
                    }
                });
                System.out.println("Job thread finishing");
            }
        }.start();
    }
}

2 级

public abstract class CallBack {
    public abstract void onRequestComplete(String response);
}

3 级

public class Job {
    public void doRequest(String request, final CallBack callBack){
        new Thread() {
            @Override
            public void run() {
                //Async Long running task
                try {
                    Thread.sleep(10000);
                    callBack.onRequestComplete("Long Async task Completed-- Sending response back");
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }.start();
    }
}
于 2014-05-09T23:22:03.863 回答
1

是和

是:Callback您为对象提供了一个实例clientonRequestComplete客户端在获得响应(来自服务器)后将简单地调用方法。

NO:如果没有更多的线程在运行并且有未完成的请求,它们可能会被取消,因为应用程序将被停止。

但我认为你问的是正常执行而不是 JVM 的关闭。

所以我的猜测是YES,但我不知道客户端的实现(我猜它启动了一个内部有某种事件机制的线程)。

于 2014-05-09T22:12:27.183 回答
1

只要您知道以下情况是正确的(当进程处于活动状态时),这样做是安全的:

  1. 每个 Job 的onRun()方法都会(最终)被执行,并且
  2. 您使用的每个异步 API 都将始终调用其回调方法(即使存在异常)。
于 2014-05-09T22:17:14.317 回答