-3

here is my piece of code:

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();

i want to say if AsyncTask past 30000 MILLISECONDS and didn't finish the job return a message, how i can code this? thanks

Milind Mevada
  • 3,145
  • 1
  • 14
  • 22
Mr Taha
  • 61
  • 9

2 Answers2

0

I would prefer doing it using an AsyncTask.

Copy-pasting from the link:

AsyncTask enables proper and easy use of the UI thread. This class allows you to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask.

Said this, configuring an AsyncTask is pretty simple, just create a class like the following:

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]);
    }
}

This is a basic example on how to create an AsyncTask, you can use it like this (form activity/fragment):

AsyncTaskExample asyncTask = new AsyncTaskExample();
asyncTask.get(30000, TimeUnit.MILLISECONDS);

This will set a timeout on your async operation. Look here for the exact Exception/return

For any further question, ask freely. Hope this helps

Edit:

I just noticed you have an AsyncTask inside your thread. Since AsyncTask is already async, I would avoid doing this and simply call the AsyncTask with the method I gave you before. In that way the AsyncTask will run up to the given TimeSpan :)

Community
  • 1
  • 1
Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66
-1

See the code below: It may help you.

CountDownTimer timer = new CountDownTimer(3000,1000) {
    @Override
    public void onTick(long l) {

    }

    @Override
    public void onFinish() {
        //return a message here;
    }
};
timer.start();

And if you have an async task. Then do like below in doInBackground method:

 @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;
    }
Gourango Sutradhar
  • 1,461
  • 10
  • 16