java - 如果异步任务时间限制

这是我的一段代码:


Thread  one = new Thread() {

    public void run() {

        try {

            new LongOperation(finalJson)

                .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)

                .get(30000, TimeUnit.MILLISECONDS);

        } catch (InterruptedException e) {

            e.printStackTrace();

        } catch (ExecutionException e) {

            e.printStackTrace();

        } catch (TimeoutException e) {

            e.printStackTrace();

        }

    }

};

one.start();

我想说如果AsyncTask超过 30000 毫秒并且没有完成工作返回一条消息,我该如何编码?谢谢


慕桂英3389331
浏览 373回答 2
2回答

开心每一天1111

我更喜欢使用AsyncTask来做。从链接复制粘贴:AsyncTask 可以正确且轻松地使用 UI 线程。此类允许您在 UI 线程上执行后台操作并发布结果,而无需操作线程和/或处理程序。AsyncTask 被设计为围绕 Thread 和 Handler 的辅助类,并不构成通用的线程框架。理想情况下,AsyncTasks 应该用于短期操作(最多几秒钟)。如果您需要让线程长时间运行,强烈建议您使用 java.util.concurrent 包提供的各种 API,例如Executor、ThreadPoolExecutor 和 FutureTask。说了这么多,配置一个 AsyncTask 还是蛮简单的,只需要创建一个像下面这样的类:private class LongOperation extends AsyncTask<String, Void, String> {&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onPreExecute() {&nbsp; &nbsp; &nbsp; &nbsp; super.onPreExecute();&nbsp; &nbsp; &nbsp; &nbsp; //this method works on the UI thread.&nbsp; &nbsp; &nbsp; &nbsp; //this is the first to run before "doInBackground"&nbsp; &nbsp; &nbsp; &nbsp; mTextView.setText("we start!");&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected String doInBackground(String... params) {&nbsp; &nbsp; &nbsp; &nbsp;try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //do whatever your async task needs to do. This method works async&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //you can also call an UI callback from here with the publishProgress method. This will call the "onProgressUpdate" method, and it has to respect his type.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; publishProgress("we go on!");&nbsp; &nbsp; &nbsp; &nbsp;} catch (InterruptedException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Thread.interrupted();&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;return "Executed";&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onPostExecute(String result) {&nbsp; &nbsp; &nbsp; //this method works on the UI thread&nbsp; &nbsp; &nbsp; //it get the "doInBackground" return value.&nbsp; &nbsp; &nbsp; mTextView.setText(result);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onPreExecute() {}&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onProgressUpdate(String... values) {&nbsp; &nbsp; &nbsp; &nbsp;//this method works on UI thread, so it can access UI components and ctx&nbsp; &nbsp; &nbsp; &nbsp;mTextView.setText(values[0]);&nbsp; &nbsp; }}这是一个关于如何创建 AsyncTask 的基本示例,您可以像这样使用它(表单活动/片段):AsyncTaskExample asyncTask = new AsyncTaskExample();asyncTask.get(30000, TimeUnit.MILLISECONDS);这将为您的异步操作设置超时。在这里查看确切的异常/返回如有任何进一步的问题,请自由提问。希望这可以帮助编辑:我只是注意到你AsyncTask的线程里面有一个。由于AsyncTask 已经是 async,我会避免这样做,只需AsyncTask使用我之前给您的方法调用。这样,AsyncTask将运行到给定的 TimeSpan :)

HUH函数

请参阅下面的代码:它可能对您有所帮助。CountDownTimer timer = new CountDownTimer(3000,1000) {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onTick(long l) {&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onFinish() {&nbsp; &nbsp; &nbsp; &nbsp; //return a message here;&nbsp; &nbsp; }};timer.start();如果你有一个异步任务。然后在doInBackground方法中执行以下操作:&nbsp;@Override&nbsp; &nbsp; protected Void doInBackground(Void... voids) {&nbsp; &nbsp; &nbsp; &nbsp;//simply do your job&nbsp; &nbsp; &nbsp; &nbsp; CountDownTimer timer = new CountDownTimer(3000,1000) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onTick(long l) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onFinish() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //return a message here;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return message;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; timer.start();&nbsp; &nbsp; &nbsp; &nbsp; return your_reult;&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java