2013年8月13日星期二

Android Threaded asynchronous fetch data from the Internet torefresh the list of issues

Interface is a list of ListView plus a refresh button Button
process:
1. clicking refresh, the new thread to retrieve data from the network, update ListView, the thread will always take data ProgressDialog prompt "reading"
2. thread finish, turn off the ProgressDialog, with the read data is not updated ListView is empty
3. If you read halfway off ProgressDialog, the thread identifier is set false, discard change thread

threaded code is as follows

private class LoadListRunnable implements Runnable {
private boolean isDrop;

public void drop() {
isDrop = true;
}

@Override
public void run() {
{ ...... } //http从网络读取数据,阻塞等待回应

if(!isDrop) {
mHandler.sendMessage(SUCCESS);
}

}

regarded each click refresh existing LoadListRunnable to drop off, re-new one LoadListRunnable come and data

old_LoadListRunnable.drop();
old_LoadListRunnable = new LoadListRunnable();
newThread(old_LoadListRunnable).start(); //也有用线程池的


The question now is, kept a "refresh -> Cancel -> Refresh -> Cancel" action words, drop off the thread is blocked waiting for the state to return data network, and even some threads have been wait state, which leads to large accumulation of thread, very resource-intensive.

java threads can not directly kill off, it is very troublesome ... respond to user requirements can solve this "Refresh -> Cancel -> Refresh -> Cancel" method of operation
----- - Solution --------------------------------------------
< br />
I have encountered this problem, I was so resolved to cancel when disconnected from the network directly linked, this time blocking the network connection thread will immediately return an error, and then restart the network link, to connect again
------ Solution ------------------------------------------- -
think it should be so that when the user clicks the Refresh button in the main thread function calls drop, drop functions also need to add the code disconnected from the network. Then the child thread to capture network anomalies and determine isDrop flag And then the child thread notify the main line through the handler thread has finish the number of days and end at the main thread again new new child thread, re-networked access to data.
This will ensure that, before the finish in a child thread will not have a new child thread appears to solve the problem.
------ For reference only -------------------------------------- -
top, tangled for a long time are not been resolved, there is an expert to give an idea what
------ For reference only --------------------------- ------------
forehead, and I are some AsyncTask to do. . .
------ For reference only -------------------------------------- -
just start a thread, do not have to start a thread each refresh
------ For reference only --------------------- ------------------


If the user cancels, this one is still blocking the thread go again? For example, in response to the egg waiting for the server, or is downloading data, but speed is relatively slow and even speed it is almost close to zero. In this case many people will stop and refresh the current link, right? Do you want to wait until after clicking the Refresh data on a data link failure or downloaded again to open a new thread to download the new data do? That this does not refresh what is the difference with?
------ For reference only -------------------------------------- -


updated list may repeatedly perform an operation, it is inappropriate to do so with AsynTask, and repeat the operation if the case or not as thread + runnable thread pool good control

------ For reference only ---------------------------------- -----
Zaiding ~
------ For reference only -------------------------------------- -
landlord how to solve this problem

没有评论:

发表评论