有一段时间,我一直在多线程环境中使用HttpClient.对于每个线程,当它启动连接时,它将创建一个全新的HttpClient实例.
最近,我发现,通过使用这种方法,它可能导致用户打开太多端口,并且大多数连接处于TIME_WAIT状态.
http://www.opensubscriber.com/message/commons-httpclient-dev@jakarta.apache.org/86045.html
因此,而不是每个线程做:
HttpClient c = new HttpClient();
try {
c.executeMethod(method);
}
catch(...) {
}
finally {
method.releaseConnection();
}
Run Code Online (Sandbox Code Playgroud)
我们计划:
[方法A]
// global_c is initialized once through
// HttpClient global_c = new HttpClient(new MultiThreadedHttpConnectionManager());
try {
global_c.executeMethod(method);
}
catch(...) {
}
finally {
method.releaseConnection();
}
Run Code Online (Sandbox Code Playgroud)
在正常情况下,global_c将同时由50个++线程访问.我想知道,这会产生任何性能问题吗?MultiThreadedHttpConnectionManager是否使用无锁机制来实现其线程安全策略?
如果10个线程正在使用global_c,那么其他40个线程是否会被锁定?
或者,如果在每个线程中我创建一个HttpClient实例,但是显式释放连接管理器会更好吗?
[方法B]
MultiThreadedHttpConnectionManager connman = new MultiThreadedHttpConnectionManager();
HttpClient c = new HttpClient(connman);
try {
c.executeMethod(method);
}
catch(...) {
}
finally {
method.releaseConnection();
connman.shutdown();
}
Run Code Online (Sandbox Code Playgroud)
connman.shutdown()会遇到性能问题吗?
对于使用50 ++线程的应用程序,我可以知道哪种方法(A或B)更好吗?
我是android的新手,并创建了一个通过Web服务与服务器通信的应用程序.服务器端代码用asp.net编写,服务器端用于维护Session.我正在使用Volley来调用网络服务,但问题是我无法在服务器端维护会话,据我所知,我必须从头部获取会话ID我已经搜索了这个网站上的分配但是我我不能成功.我的问题是在整个应用程序中维护会话.下面是我在android中调用web服务的代码.
_Login_Webservice = new JsonObjectRequest(Request.Method.GET,
_Login_Url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// Log.e(Utils.tag, "response :" + response);
User _User;
button.setLoadingState(false);
try {
boolean Success = response.getBoolean("Success");
if (Success == true) {
//my code
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("", "exception in Login User Login_Activity: ");
Log.e("", "error is : " + error.toString());
}
}) {
@Override
protected …Run Code Online (Sandbox Code Playgroud)