首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用java.util.concurrent包实现后台线程?

如何使用java.util.concurrent包实现后台线程?
EN

Stack Overflow用户
提问于 2020-11-07 13:43:14
回答 1查看 803关注 0票数 1

这是我第一次使用的代码,但在最新的安卓版本中,AsyncTask类被弃用,因此它没有响应,然后我使用了Thread类,但该类也不起作用。我希望得到与AsyncTask类相同的结果。我知道我必须使用java.util.concurrent包的某个executor类,但不知道该使用哪个和如何使用它。请帮我弄一下这个东西。

代码语言:javascript
复制
private static final String USGS_REQUEST_URL =
            "https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2016-01-01&endtime=2016-05-02&minfelt=50&minmagnitude=5";

EarthquakeAsyncTask task = new EarthquakeAsyncTask();
        task.execute(USGS_REQUEST_URL);
private class EarthquakeAsyncTask extends AsyncTask<String, Void, Event> {

        @Override
        protected Event doInBackground(String... urls) {

            // Perform the HTTP request for earthquake data and process the response.
            Event result = Utils.fetchEarthquakeData(urls[0]);
            return result;
        }

        @Override
        protected void onPostExecute(Event result) {
            // Update the information displayed to the user.
            updateUi(result);
        }
    }
代码语言:javascript
复制
private static final String USGS_REQUEST_URL =
            "https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2016-01-01&endtime=2016-05-02&minfelt=50&minmagnitude=5";

earthquakeRunnable runnable = new earthquakeRunnable(USGS_REQUEST_URL);
        runnable.start();

private class earthquakeRunnable extends Thread{

            String urls;
            earthquakeRunnable(String url){
                this.urls = url;
            }
            @Override
            public void run() {
                // Perform the HTTP request for earthquake data and process the response.
                Event result = Utils.fetchEarthquakeData(urls);
                // Update the information displayed to the user
                updateUi(result);
            }
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-08 17:38:38

下面是一个如何在您的活动/片段中使用ExecutorService的示例:

代码语言:javascript
复制
// Create some member variables for the ExecutorService 
// and for the Handler that will update the UI from the main thread
ExecutorService mExecutor = Executors.newSingleThreadExecutor();
Handler mHandler = new Handler(Looper.getMainLooper());

// Create an interface to respond with the result after processing
public interface OnProcessedListener {
    public void onProcessed(Event result);
}

private void processInBg(final String url, final boolean finished){
    
    final OnProcessedListener listener = new OnProcessedListener(){
        @Override
        public void onProcessed(Event result){
            // Use the handler so we're not trying to update the UI from the bg thread
            mHandler.post(new Runnable(){
                @Override
                public void run(){
                    // Update the UI here
                    updateUi(result);
                    
                    // ...
                    
                    // If we're done with the ExecutorService, shut it down.
                    // (If you want to re-use the ExecutorService,
                    // make sure to shut it down whenever everything's completed
                    // and you don't need it any more.)
                    if(finished){
                        mExecutor.shutdown();
                    }
                }
            });
        }
    };
    
    Runnable backgroundRunnable = new Runnable(){
        @Override
        public void run(){
            // Perform your background operation(s) and set the result(s)
            Event result = Utils.fetchEarthquakeData(url);
            
            // ...
            
            // Use the interface to pass along the result
            listener.onProcessed(result);
        }
    };
    
    mExecutor.execute(backgroundRunnable);
}

然后,在需要触发后台处理的任何地方:

代码语言:javascript
复制
processInBg("some_url", true);

根据您的情况,您需要定制您的ExecutorService实现以更好地满足您的需求。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64724824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档