java - Set a Timeout to a Thread Class -
hy!!
i have thread class , want set timeout inside after 10 sec.
how been made?
class:
public class httpconnection extends thread{ list<namevaluepair> list; string url; handler handler; public httpconnection(list<namevaluepair> params, string url, handler handler) { this.list = params; this.url = url; this.handler = handler; } @override public void run() { try { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); string result; bufferedreader in = null; httppost.setentity(new urlencodedformentity(this.list)); // execute http post request httpresponse response = httpclient.execute(httppost); if(response != null){ in = new bufferedreader(new inputstreamreader(response.getentity().getcontent())); stringbuffer sb = new stringbuffer(""); string line = ""; string nl = system.getproperty("line.separator"); while ((line = in.readline()) != null) { sb.append(line + nl); } in.close(); message msg = message.obtain(); if ((result = sb.tostring()) != null) { msg.obj = result; } else { msg.obj = null; throw new exception("error"); } handler.sendmessage(msg); } } catch (exception e) { log.e("xxx", e.getmessage()); } super.run(); } }
in java should able use threadpoolexecutor's awaittermination method set timeout. whichever class creating , executing thread should able call awaittermination on executor 10 seconds. trying (set timeout on or within thread)?
threadpoolexecutor.awaittermination(10, timeunit.seconds);
Comments
Post a Comment