Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java-make if for异步任务时间限制_Java_Android - Fatal编程技术网

java-make if for异步任务时间限制

java-make if for异步任务时间限制,java,android,Java,Android,以下是我的代码: Thread one = new Thread() { public void run() { try { new LongOperation(finalJson) .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR) .get(30000, TimeUnit.MILLISECONDS); } catc

以下是我的代码:

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毫秒并且没有完成作业,则返回一条消息,我如何编写此代码?谢谢

查看下面的代码:它可能会帮助您

如果你有一个异步任务。然后在doInBackground方法中执行以下操作:


我更喜欢用一种新的方法

从链接复制粘贴:

AsyncTask允许正确且轻松地使用UI线程。这个班 允许您在上执行后台操作和发布结果 UI线程,而无需操作线程和/或处理程序

AsyncTask被设计成一个围绕线程和处理程序的助手类 并且不构成通用线程框架。异步任务 理想情况下,应在最短时间内用于几秒钟的短期操作 最如果需要让线程长时间运行, 强烈建议您使用 并发包,如Executor、ThreadPoolExecutor和 未来任务

尽管如此,配置AsyncTask非常简单,只需创建如下类:

private class LongOperation extends AsyncTask<String, Void, String> {


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //this method works on the UI thread.
        //this is the first to run before "doInBackground"
        mTextView.setText("we start!");
    }

    @Override
    protected String doInBackground(String... params) {
       try {
          //do whatever your async task needs to do. This method works async
          //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.
          publishProgress("we go on!");
       } catch (InterruptedException e) {
         Thread.interrupted();
       }
       return "Executed";
    }

    @Override
    protected void onPostExecute(String result) {
      //this method works on the UI thread
      //it get the "doInBackground" return value.
      mTextView.setText(result);
    }

    @Override
    protected void onPreExecute() {}

    @Override
    protected void onProgressUpdate(String... values) {
       //this method works on UI thread, so it can access UI components and ctx
       mTextView.setText(values[0]);
    }
}
这将为异步操作设置超时。对于确切的异常/返回

如有任何进一步的问题,请自由提问。希望这有帮助

编辑:
我刚刚注意到你的线程中有一个异步任务。由于AsyncTask已经是异步的,所以我将避免这样做,而只需使用我之前提供的方法调用AsyncTask。这样,AsyncTask将运行到给定的时间跨度:

好的,先生,谢谢您的提示,您做得很好,问题是什么?您的代码示例使用了一个线程,不是您在问题中所问的AsyncTask。@代码学徒他在线程内使用AsyncTask,这可能不正确。应该是AsyncTask内的线程。我正在与服务器交谈,但我有一个时间限制。我想说的是,如果我们超过了该时间,请将所有内容设为空,并返回一条消息,说明您在做什么会有帮助的
 @Override
    protected Void doInBackground(Void... voids) {
       //simply do your job
        CountDownTimer timer = new CountDownTimer(3000,1000) {
            @Override
            public void onTick(long l) {

            }

            @Override
            public void onFinish() {
                //return a message here;
                return message;
            }
        };
        timer.start();
        return your_reult;
    }
private class LongOperation extends AsyncTask<String, Void, String> {


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //this method works on the UI thread.
        //this is the first to run before "doInBackground"
        mTextView.setText("we start!");
    }

    @Override
    protected String doInBackground(String... params) {
       try {
          //do whatever your async task needs to do. This method works async
          //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.
          publishProgress("we go on!");
       } catch (InterruptedException e) {
         Thread.interrupted();
       }
       return "Executed";
    }

    @Override
    protected void onPostExecute(String result) {
      //this method works on the UI thread
      //it get the "doInBackground" return value.
      mTextView.setText(result);
    }

    @Override
    protected void onPreExecute() {}

    @Override
    protected void onProgressUpdate(String... values) {
       //this method works on UI thread, so it can access UI components and ctx
       mTextView.setText(values[0]);
    }
}
AsyncTaskExample asyncTask = new AsyncTaskExample();
asyncTask.get(30000, TimeUnit.MILLISECONDS);