How to cancel a running thread

Source: Internet
Author: User

============ Problem Description ============


What I want to do now is to click on one of the 9 buttons on the left to create 10 threads to request 10 pictures from the server, and the trouble I have now is that when I click the left button quickly and randomly, it doesn't create dozens of or even hundreds of threads (tested, too many threads directly causing the phone to die), And I just want the last click of the button to request the picture. Everybody help me to think how to do ah, I was the only one thread to create a request after a picture, then to create a thread to request the next one, or how to do ah! I don't know how to kill a running thread, note: The thread is not looping.
What good ideas are we talking about?

============ Solution 1============


10 threads too many, it is recommended that you use a thread pool to open 4, 5 threads can be reused after all, the thread consumes CPU and memory.
Threads cannot be stopped immediately, you can add a flag bit inside the thread if the threads download the picture successfully without looping,
If you use a thread pool then you don't need it, you can take advantage of threads for a long time and probably open 2 threads.

============ Solution 2============


Use the thread pool, unless it's a high priority, or it's usually a thread pool.

Usage reference this article

============ Solution 3============


quoted landlord Jhonzengxuejun reply:
I now want to implement the function is to click on the left 9 buttons in any one, create 10 threads to the server to request 10 pictures, I now have trouble is, When I click on the left button quickly and randomly, it doesn't create dozens of or even hundreds of threads (tested, too many threads directly cause the phone to die), and I just want the last click of the button requested image. Everybody help me to think how to do ah, I was the only one thread to create a request after a picture, then to create a thread to request the next one, or how to do ah! I don't know how to kill a running thread, note: The thread is not looping.
What good ideas are we talking about?


Since you are 10 threads, then you have to judge that these 10 threads are still in the absence, if it is, either do not allow the request, or the request queue ...

Of course upstairs said the thread pool, is also a way,

============ Solution 4============


You can use Intentservice to start the background service load thread, this will only one load, the end of loading one will be a thread off

============ Solution 5============


can submit execution Fututask
Executorservice AA = Executors.newcachedthreadpool (); Futuretask<string> a = new futuretask<string> (new callable<string> () {@Overridepublic String call ()    Throws Exception {return null;}  });  Aa.submit (a); A.cancel (TRUE);

How to cancel a running thread

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.