How to cancel the previous thread of multiple asynchronous task threads

Source: Internet
Author: User

How to cancel the previous thread of multiple asynchronous task threads

This problem occurs in the app. When a user clicks a tag, an asynchronous task is used to open a subthread to request data from the server and refresh the display.


However, it is inevitable that some users like to play and work hard. As a programmer, We have to tolerate users. Therefore, we adopt the delayed message sending mechanism. If the user clicks the last message, that is, the last message has not been sent, and another message is sent to the handler, we can remove the previous message.


The Code is as follows:

Handler1.removeMessages (1000 );

Handler1.sendEmptyMessageDelayed (1000,500 );


After the message is sent, an asynchronous task is executed. In this case, a new task is generated after the previous task is not completed. Obviously, the previous task is useless and needs to be canceled (if any ).

Sample Code:

If (getCourseTask! = Null &&! GetCourseTask. isCancelled () {// getCourseTask is an asynchronous task
GetCourseTask. cancel (true );
GetCourseTask = null;
PullToRefreshView. onRefreshComplete (); // indicates the restoration of the interface.
}
A design concept is required for java multi-thread asynchronous execution?

First, you use the background thread. The background thread is attached to the main thread. Once the main thread ends, the background thread will be destroyed.
Second, you should not limit yourself to this idea. You can use the most classic "producer and consumer model ".
You can understand the producer and consumer.
First, you open a non-Background thread in the main thread to execute the query, and the query result will be placed in a queue.
You need to use this data query to read the query results from the queue.
Your idea should be relatively simple.
Give you a simple process
Data Query module A is A producer,
The Data Module B is a consumer,
1. The main thread calls A to generate data and put it in queue C,
2. Wake up all threads waiting on C (here only B is waiting)
3. B is awakened, reads data from C, and displays the data. After the data is displayed, wait on the C object,
And then cyclically to 1, 2, 3.

Shoupai ~~

What is the role of multithreading Asynchronization?

Asynchronous and multithreading are two concepts
Multi-threaded processing of up to one task at the same time (macro, depending on the number of processor cores)
Asynchronous means: for example, if Clerk A is shopping, Clerk B needs to get something for clerk A. At this time, clerk A can continue reading things, while Clerk B can get things. wait for B to receive A notification. in this process, A can continue to work and notify A after B completes. Of course, asynchronous implementation depends on multithreading. Because there is asynchronous callback

Related Article

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.