V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  PixelCode  ›  全部回复第 6 页 / 共 17 页
回复总数  323
1 ... 2  3  4  5  6  7  8  9  10  11 ... 17  
2018 年 9 月 26 日
回复了 PixelCode 创建的主题 Java 这是死锁?
@xixinimei

okhttp-3.11.0.jar 应该是 3.11.0 版本

3 楼你说的我有点晕,意思是 jstack 打印的内容里的:
"OkHttp ConnectionPool" daemon prio=10 tid=0x00007f5b2c300000 nid=0x752 in Object.wait() [0x00007f5af35f4000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
是没有问题的是吗?可以不用管?

然后是我自己写的方法,调用一次 sendPostOKHttp()方法就创建一个 OkHttpClient,然后由于 OkHttpClient 对象太多,就有 N 个线程(还是线程池)?这样理解对吗?

*****

所以我的代码要改一下:
把 OkHttpClient client = new OkHttpClient();定义到类成员变量,然后每次使用这个对象调用剩下的代码?
这样理解对吗?
2018 年 9 月 26 日
回复了 PixelCode 创建的主题 Java 这是死锁?
@xixinimei 怎么会 wait 呢,啥事都没做,就用 okhttp 发送 http 请求,怎么这么多 wait 的,TCP 连接数不断上涨,感觉跟这 okhttp 有很大关系。

代码:

/**
* 向指定 URL 发送 POST 方法的请求
*
* @param url 发送请求的 URL
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
public static String sendPostOKHttp(String url, String param) {

logger.info("sendPostOKHttp()");
OkHttpClient client = new OkHttpClient();

// 1.构建 RequestBody
RequestBody body = RequestBody.create(MediaType.parse("text/xml;charset=UTF-8"),param);

Request request = new Request.Builder().url(url).post(body).build();
Response response = null;
try {
response = client.newCall(request).execute();
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}

/**
* 处理 http 请求( GET/POST )
*
* @param reqUrl 请求地址
* @param reqMethod 请求方式( GET/POST )
* @param outputStr 参数( JSON String )
* @return
*/
public static String httpReqOKHttp(String reqUrl, String reqMethod, String outputStr) {
logger.info("httpReqOKHttp()");

String returnStr = "";

if ("POST".equalsIgnoreCase(reqMethod)) {
MediaType JSON = MediaType.get("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(JSON, outputStr);
Request request = new Request.Builder().url(reqUrl).post(body).build();
Response response = null;
try {
response = client.newCall(request).execute();
returnStr = response.body().string();
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getStackTrace());
}
}

if ("GET".equalsIgnoreCase(reqMethod)) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(reqUrl).build();

Response response = null;
try {
response = client.newCall(request).execute();
returnStr = response.body().string();
} catch (IOException e) {
logger.error(e.getStackTrace());
}
}
return returnStr;
}

/**
* 向指定 URL 发送 GET 方法的请求
*
* @param url 发送请求的 URL
* @return URL 所代表远程资源的响应结果
*/
public static String sendGetOKHttp(String url) {
logger.info("sendGetOKHttp()");
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).build();

Response response = null;
try {
response = client.newCall(request).execute();
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
}

return "";
}
2017 年 11 月 30 日
回复了 k9982874 创建的主题 分享发现 山东移动成功转 8 元 4G 飞享套餐
正在尝试,成功过来更新,hhh
Play! Framework 1.x
ActFramework
给钱也没用的
2017 年 5 月 26 日
回复了 assad 创建的主题 汽车 18-23 万,买个什么车比较好?
4008
2017 年 5 月 12 日
回复了 jason19659 创建的主题 职场话题 一个同学,来上海三个月了,待业
这简历换谁都不会要
2017 年 5 月 12 日
回复了 beakey 创建的主题 问与答 ofo 客服明确说不给退押金,去哪里投诉?
我中的奖快一个月了也杳无音讯。
2017 年 5 月 11 日
回复了 zjin42 创建的主题 酷工作 速递服务类 App 招技术合伙人, 有创业想法的来看看
好像找了有俩月了吧,找不到合适的可以走招聘途径。
只是好奇你的项目,绑定一下。
;-)
2017 年 5 月 11 日
回复了 Livid 创建的主题 使用指南 对云计算公司在 V2EX 进行推广活动的一些建议
因为发这些的都是不懂技术的运营
GitHub Pages 不要钱 😝
2017 年 3 月 27 日
回复了 jswh 创建的主题 分享发现 一个简单的个人主页
又到了贴广告时间?
http://openbra.in/
2017 年 3 月 27 日
回复了 keinx 创建的主题 程序员 别用 CNZZ,有毒! entry.ximeifang.com
好像没看到楼主说的, http://openbra.in

![屏幕快照 2017-03-27 11.14.39.png]( https://ooo.0o0.ooo/2017/03/27/58d883b9930f9.png)
2017 年 3 月 24 日
回复了 henryshen233 创建的主题 Linux 新组装了台电脑,一直开机危险吗
开着呗,之前玩传奇,为了挂机,开了几个月,没吊事
1 ... 2  3  4  5  6  7  8  9  10  11 ... 17  
关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   1814 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 39ms · UTC 15:58 · PVG 23:58 · LAX 07:58 · JFK 10:58
♥ Do have faith in what you're doing.