android asynctask - Concurrent asynchronous execution more task -
hello,i want send messages more ips using tcp,i want messages concurrent asynchronous send ips,not 1 one.
@override protected boolean doinbackground(string... params) { for(int i=0;i<commonutil.iplist.size();i++){ if(!commonutil.iplist.get(i).equals(fromip)){ tcpudpcommunicate.sendsinglemessage(commonutil.iplist.get(i), commonutil.local_port, xmldata); return true; }else { log.i("ee","commonutil.iplist.get(i) null"); return false; }
if use .net 4.0 or above, use parallel.foreach()
. should free concerns of batching tasks on fixed number of threads.
look here : http://msdn.microsoft.com/en-us/library/dd460720%28v=vs.110%29.aspx
the code this.
int notfromipcount = 0; parallel.foreach(commonutil.iplist, ip => { bool isnotfromip = !ip.equals(fromip); if (isnotfromip) { tcpudpcommunicate.sendsinglemessage(ip, commonutil.local_port, xmldata); interlocked.increment(ref notfromipcount); } else { // log message } } return notfromipcount > 0;
the task parallel library bit slow debugger attached, profile in release mode without debugger , should barely see performance difference compared manually created threads.
ps. log message confusing. if commonutil.iplist.get(i)
null
, code have raised nullreferenceexception
.
Comments
Post a Comment