简体   繁体   中英

Android - How to Run Multiple Threads in Parallel

I am developing an android application that synchronizes the user's emails using Gmail API.

To do so i have implemented two services where the first is for downloading the Unread Emails and the second is for downloading Read Emails. Each service implements the Runnable interface and the code for retrieving the emails and storing them in the database is handled using a thread inside the Run() method.

Everything works fine the only problem i have is that when the Thread responsible for downloading the Read Emails is running (which takes longer than the other) the second Thread (Sync unread emails) is blocked until the first one is completed.

I would like to know if there is anyway that i can prioritize the second Thread over the first one so whenever the first one is running and the second thread have to run, the code should put the second thread on hold until the first one (Unread emails) complete running and then resume the thread on hold.

Regards.

If you would like to create a second service that runs for just a short time i would suggest you to study the AsyncTask, but if you are looking to create a service that runs constantly you would need to use the java.util.concurrent API(Executor, ThreadpoolExecutor). Wish i helped, but with a few readings you can pick up more knowledge about multithreaded applicarions. Tell me if you need more help!

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