简体   繁体   中英

Android Networking and Threading

I have a class that only contains static functions for various database queries. Since they are all network-related, I need to execute that code in another thread. I am trying to find the best way to implement this.

public class MyClass {

    public static void someFunction() {
        ...
    }

    public static void anotherFunction() {
        ...
    }

}

I was thinking of doing it this way:

public class MyClass {

    public static void someFunction() {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                ...
            }
        }
    }

    public static void anotherFunction() {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                ...
            }
        }
    }

}

Or create a new thread when I call these functions:

new AsyncTask<Void, Void, Void>() {
    @Override
    protected Void doInBackground(Void... params) {
        MyClass.someFunction();
    }
}

new AsyncTask<Void, Void, Void>() {
    @Override
    protected Void doInBackground(Void... params) {
        MyClass.anotherFunction();
    }
}

Finally, I was wondering if there's a way to run a single thread parallel to the main thread that would exclusively handle these function calls. The main thread would call these functions and the other thread would run them.

Does anyone have any ideas for the best way to implement this? Thanks!

Standard Java Concurrency utilities would fit this scenario the best.

For example:

private static final ExecutorService exe = Executors.newSingleThreadExecutor();
...
exe.execute(new Runnable() {
    public void run() {
        ...
        someFunction();
        ...
    }
});

Read about ExecutorService . ExecutorService allows you to manage threads as per your requirement. If you want your threads to spawn sequentially you could simply use ExecutorService ex = Executors.newSingleThreadExecutor() and consequentally add all your threads to this single executorService.

ex.submit(runnableInstance)

However, I've also been at exact same problem which you are facing. I also used threads to query from database. However, it made my code look messy. I would recommend you to use queries in main thread if they involve simple operations. It hardly brings any lag to app responsiveness. That ways, your code would be quite managable.

EDIT :

Long time ago, I also read about CursorLoader class that allows you to run to your queries in background and can be used by implementing LoaderManager.LoaderCallbacks<Cursor> . That ways, you don't have to write to ugly code to execute every query in background. However, to use this you need to wrap your database in a ContentProvider .

I believe easiest solution for you would be to use IntentService

Extract from android doc

All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time. 

You just need to process onHandleIntent

It does lot of work for you and as it is a subclass of service, you can bound to the service directly or other way (Intent..etc) to get back data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM