首页   注册   登录

huxiweng

https://crosslee.github.io/
  •   https://crosslee.github.io/
    V2EX 第 56945 号会员,加入于 2014-02-27 09:56:20 +08:00
    ohfiner
    huxiweng 最近回复了
    26 天前
    回复了 huxiweng 创建的主题 Java 这是死锁?
    @xixinimei

    改成下面单例,好像死锁了。。。


    private static OkHttpClient okHttpClient = null;


    private HttpTool() {
    }

    public static OkHttpClient getInstance() {
    if (okHttpClient == null) {
    //加同步安全
    synchronized (HttpTool.class) {
    if (okHttpClient == null) {
    //判空 为空创建实例
    okHttpClient = new OkHttpClient();
    }
    }

    }

    return okHttpClient;
    }


    jstack 日志:
    2018-09-27 08:01:43
    Full thread dump Java HotSpot(TM) 64-Bit Server VM (24.80-b11 mixed mode):

    "Attach Listener" daemon prio=10 tid=0x00007f5d1c001000 nid=0x11ba runnable [0x0000000000000000]
    java.lang.Thread.State: RUNNABLE

    "Okio Watchdog" daemon prio=10 tid=0x00007f5d14924800 nid=0x117c in Object.wait() [0x00007f5d20e12000]
    java.lang.Thread.State: TIMED_WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000007889c4fd8> (a java.lang.Class for okio.AsyncTimeout)
    at okio.AsyncTimeout$Companion.awaitTimeout$jvm(AsyncTimeout.kt:341)
    at okio.AsyncTimeout$Watchdog.run(AsyncTimeout.kt:228)
    - locked <0x00000007889c4fd8> (a java.lang.Class for okio.AsyncTimeout)

    "Java2D Disposer" daemon prio=10 tid=0x00007f5d1491e800 nid=0x110d in Object.wait() [0x00007f5d20131000]
    java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x0000000788ba7c48> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)
    - locked <0x0000000788ba7c48> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151)
    at sun.java2d.Disposer.run(Disposer.java:145)
    at java.lang.Thread.run(Thread.java:745)

    "New I/O worker #8" prio=10 tid=0x00007f5d0c014000 nid=0x110c runnable [0x00007f5d2070b000]
    java.lang.Thread.State: RUNNABLE
    at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
    at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
    at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:79)
    at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:87)
    - locked <0x00000007fbf15d98> (a sun.nio.ch.Util$2)
    - locked <0x00000007fbf15d88> (a java.util.Collections$UnmodifiableSet)
    - locked <0x00000007fbf02948> (a sun.nio.ch.EPollSelectorImpl)
    at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:98)
    at org.jboss.netty.channel.socket.nio.SelectorUtil.select(SelectorUtil.java:52)
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:208)
    at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:38)
    at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:102)
    at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

    "New I/O worker #7" prio=10 tid=0x00007f5d0c011800 nid=0x110b runnable [0x00007f5d2080c000]
    java.lang.Thread.State: RUNNABLE
    at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
    at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
    at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:79)
    at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:87)
    - locked <0x000000078859c718> (a sun.nio.ch.Util$2)
    - locked <0x000000078859c708> (a java.util.Collections$UnmodifiableSet)
    - locked <0x000000078855fb30> (a sun.nio.ch.EPollSelectorImpl)
    at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:98)
    at org.jboss.netty.channel.socket.nio.SelectorUtil.select(SelectorUtil.java:52)
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:208)
    at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:38)
    at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:102)
    at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

    "New I/O worker #6" prio=10 tid=0x00007f5d0c010000 nid=0x110a runnable [0x00007f5d2090d000]
    java.lang.Thread.State: RUNNABLE
    at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
    at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
    at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:79)
    at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:87)
    - locked <0x00000007fbf0fe28> (a sun.nio.ch.Util$2)
    - locked <0x00000007fbf0fe18> (a java.util.Collections$UnmodifiableSet)
    - locked <0x00000007fbf0d818> (a sun.nio.ch.EPollSelectorImpl)
    at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:98)
    at org.jboss.netty.channel.socket.nio.SelectorUtil.select(SelectorUtil.java:52)
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:208)
    at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:38)
    at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:102)
    at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

    "New I/O worker #5" prio=10 tid=0x00007f5d0c003800 nid=0x1109 runnable [0x00007f5d20a0e000]
    java.lang.Thread.State: RUNNABLE
    at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
    at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
    at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:79)
    at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:87)
    - locked <0x00000007885c1230> (a sun.nio.ch.Util$2)
    - locked <0x00000007885c1220> (a java.util.Collections$UnmodifiableSet)
    - locked <0x00000007885b1078> (a sun.nio.ch.EPollSelectorImpl)
    at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:98)
    at org.jboss.netty.channel.socket.nio.SelectorUtil.select(SelectorUtil.java:52)
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:208)
    at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:38)
    at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:102)
    at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
    27 天前
    回复了 huxiweng 创建的主题 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();定义到类成员变量,然后每次使用这个对象调用剩下的代码?
    这样理解对吗?
    27 天前
    回复了 huxiweng 创建的主题 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 "";
    }
    327 天前
    回复了 k9982874 创建的主题 分享发现 山东移动成功转 8 元 4G 飞享套餐
    正在尝试,成功过来更新,hhh
    Play! Framework 1.x
    ActFramework
    2017-09-06 14:17:02 +08:00
    回复了 xuezher 创建的主题 程序员 朋友公司的数据遭到勒索病毒攻击,黑客张嘴就要 2 个币....
    给钱也没用的
    2017-05-12 10:59:41 +08:00
    回复了 jason19659 创建的主题 职场话题 一个同学,来上海三个月了,待业
    这简历换谁都不会要
    关于   ·   FAQ   ·   API   ·   我们的愿景   ·   广告投放   ·   感谢   ·   实用小工具   ·   743 人在线   最高记录 3762   ·  
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.1 · 7ms · UTC 19:21 · PVG 03:21 · LAX 12:21 · JFK 15:21
    ♥ Do have faith in what you're doing.
    沪ICP备16043287号-1