Unirest异步请求阻止JVM退出

时间:2014-12-20 08:51:11

标签: java asynchronous jvm unirest

我试图通过Java中的unirest来使用异步回调。但是,从同步切换到异步请求后,JVM似乎无法退出。我不确定线程​​转储是什么意思。

我编译&在Mac OS X 10.9.5上的JDK 1.8.0_25上运行它 -

00:16 $ java -version
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

这是代码 -

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.async.Callback;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.mashape.unirest.request.GetRequest;

public class SampleTestCase {

    public static void main(String[] args) {
        System.out.println("Starting...");
        GetRequest reqfuture;
        reqfuture = Unirest.get("http://www.google.com");

        reqfuture.asStringAsync(new Callback<String>() {
            @Override
            public void completed(HttpResponse<String> httpResponse) {
                System.err.println("Completed");
            }

            @Override
            public void failed(UnirestException e) {
                System.err.println("Failed");

            }

            @Override
            public void cancelled() {
                System.err.println("Cancelled");

            }
        });
        System.out.print("Exiting...");
    }
}

这是程序输出 -

Starting...
Exiting...Completed
2014-12-20 00:41:30

这是线程转储 -

Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.25-b02 mixed mode):

"DestroyJavaVM" #23 prio=5 os_prio=31 tid=0x00007fdcac85e000 nid=0x1903 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"I/O dispatcher 8" #22 prio=5 os_prio=31 tid=0x00007fdcac08b000 nid=0x7103 runnable [0x000000012d9c5000]
   java.lang.Thread.State: RUNNABLE
    at sun.nio.ch.KQueueArrayWrapper.kevent0(Native Method)
    at sun.nio.ch.KQueueArrayWrapper.poll(KQueueArrayWrapper.java:198)
    at sun.nio.ch.KQueueSelectorImpl.doSelect(KQueueSelectorImpl.java:103)
    at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
    - locked <0x000000076bfc7b80> (a sun.nio.ch.Util$2)
    - locked <0x000000076bfc7b70> (a java.util.Collections$UnmodifiableSet)
    - locked <0x000000076bfc7a50> (a sun.nio.ch.KQueueSelectorImpl)
    at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:256)
    at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:105)
    at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:584)
    at java.lang.Thread.run(Thread.java:745)

"I/O dispatcher 7" #21 prio=5 os_prio=31 tid=0x00007fdcab998000 nid=0x6f03 runnable [0x000000012d8c2000]
   java.lang.Thread.State: RUNNABLE
    at sun.nio.ch.KQueueArrayWrapper.kevent0(Native Method)
    at sun.nio.ch.KQueueArrayWrapper.poll(KQueueArrayWrapper.java:198)
    at sun.nio.ch.KQueueSelectorImpl.doSelect(KQueueSelectorImpl.java:103)
    at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
    - locked <0x000000076bfc7810> (a sun.nio.ch.Util$2)
    - locked <0x000000076bfc7800> (a java.util.Collections$UnmodifiableSet)
    - locked <0x000000076bfc76e0> (a sun.nio.ch.KQueueSelectorImpl)
    at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:256)
    at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:105)
    at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:584)
    at java.lang.Thread.run(Thread.java:745)

"I/O dispatcher 6" #20 prio=5 os_prio=31 tid=0x00007fdcaf017000 nid=0x6d03 runnable [0x000000012d7bf000]
   java.lang.Thread.State: RUNNABLE
    at sun.nio.ch.KQueueArrayWrapper.kevent0(Native Method)
    at sun.nio.ch.KQueueArrayWrapper.poll(KQueueArrayWrapper.java:198)
    at sun.nio.ch.KQueueSelectorImpl.doSelect(KQueueSelectorImpl.java:103)
    at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
    - locked <0x000000076bfc74a0> (a sun.nio.ch.Util$2)
    - locked <0x000000076bfc7490> (a java.util.Collections$UnmodifiableSet)
    - locked <0x000000076bfc7370> (a sun.nio.ch.KQueueSelectorImpl)
    at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:256)
    at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:105)
    at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:584)
    at java.lang.Thread.run(Thread.java:745)

"I/O dispatcher 5" #19 prio=5 os_prio=31 tid=0x00007fdcaf016000 nid=0x6b03 runnable [0x000000012d6bc000]
   java.lang.Thread.State: RUNNABLE
    at sun.nio.ch.KQueueArrayWrapper.kevent0(Native Method)
    at sun.nio.ch.KQueueArrayWrapper.poll(KQueueArrayWrapper.java:198)
    at sun.nio.ch.KQueueSelectorImpl.doSelect(KQueueSelectorImpl.java:103)
    at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
    - locked <0x000000076bfc7130> (a sun.nio.ch.Util$2)
    - locked <0x000000076bfc7120> (a java.util.Collections$UnmodifiableSet)
    - locked <0x000000076bfc7000> (a sun.nio.ch.KQueueSelectorImpl)
    at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:256)
    at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:105)
    at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:584)
    at java.lang.Thread.run(Thread.java:745)

"I/O dispatcher 4" #18 prio=5 os_prio=31 tid=0x00007fdcab94c800 nid=0x6903 runnable [0x000000012d5b9000]
   java.lang.Thread.State: RUNNABLE
    at sun.nio.ch.KQueueArrayWrapper.kevent0(Native Method)
    at sun.nio.ch.KQueueArrayWrapper.poll(KQueueArrayWrapper.java:198)
    at sun.nio.ch.KQueueSelectorImpl.doSelect(KQueueSelectorImpl.java:103)
    at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
    - locked <0x000000076bfc6dc0> (a sun.nio.ch.Util$2)
    - locked <0x000000076bfc6db0> (a java.util.Collections$UnmodifiableSet)
    - locked <0x000000076bfc6c90> (a sun.nio.ch.KQueueSelectorImpl)
    at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:256)
    at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:105)
    at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:584)
    at java.lang.Thread.run(Thread.java:745)

"I/O dispatcher 3" #17 prio=5 os_prio=31 tid=0x00007fdcab94b000 nid=0x6703 runnable [0x000000012d4b6000]
   java.lang.Thread.State: RUNNABLE
    at sun.nio.ch.KQueueArrayWrapper.kevent0(Native Method)
    at sun.nio.ch.KQueueArrayWrapper.poll(KQueueArrayWrapper.java:198)
    at sun.nio.ch.KQueueSelectorImpl.doSelect(KQueueSelectorImpl.java:103)
    at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
    - locked <0x000000076bfc6a50> (a sun.nio.ch.Util$2)
    - locked <0x000000076bfc6a40> (a java.util.Collections$UnmodifiableSet)
    - locked <0x000000076bfc6920> (a sun.nio.ch.KQueueSelectorImpl)
    at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:256)
    at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:105)
    at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:584)
    at java.lang.Thread.run(Thread.java:745)

"I/O dispatcher 2" #16 prio=5 os_prio=31 tid=0x00007fdcaf015800 nid=0x6503 runnable [0x000000012d3b3000]
   java.lang.Thread.State: RUNNABLE
    at sun.nio.ch.KQueueArrayWrapper.kevent0(Native Method)
    at sun.nio.ch.KQueueArrayWrapper.poll(KQueueArrayWrapper.java:198)
    at sun.nio.ch.KQueueSelectorImpl.doSelect(KQueueSelectorImpl.java:103)
    at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
    - locked <0x000000076bfc66e0> (a sun.nio.ch.Util$2)
    - locked <0x000000076bfc66d0> (a java.util.Collections$UnmodifiableSet)
    - locked <0x000000076bfc65b0> (a sun.nio.ch.KQueueSelectorImpl)
    at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:256)
    at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:105)
    at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:584)
    at java.lang.Thread.run(Thread.java:745)

"I/O dispatcher 1" #15 prio=5 os_prio=31 tid=0x00007fdcac82a000 nid=0x6303 runnable [0x000000012d2b0000]
   java.lang.Thread.State: RUNNABLE
    at sun.nio.ch.KQueueArrayWrapper.kevent0(Native Method)
    at sun.nio.ch.KQueueArrayWrapper.poll(KQueueArrayWrapper.java:198)
    at sun.nio.ch.KQueueSelectorImpl.doSelect(KQueueSelectorImpl.java:103)
    at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
    - locked <0x000000076bfc6370> (a sun.nio.ch.Util$2)
    - locked <0x000000076bfc6360> (a java.util.Collections$UnmodifiableSet)
    - locked <0x000000076bfc6240> (a sun.nio.ch.KQueueSelectorImpl)
    at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:256)
    at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:105)
    at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:584)
    at java.lang.Thread.run(Thread.java:745)

"Thread-2" #14 daemon prio=5 os_prio=31 tid=0x00007fdcae070800 nid=0x6103 in Object.wait() [0x000000012d1ad000]
   java.lang.Thread.State: TIMED_WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x000000076bee9e10> (a com.mashape.unirest.http.async.utils.AsyncIdleConnectionMonitorThread)
    at com.mashape.unirest.http.async.utils.AsyncIdleConnectionMonitorThread.run(AsyncIdleConnectionMonitorThread.java:22)
    - locked <0x000000076bee9e10> (a com.mashape.unirest.http.async.utils.AsyncIdleConnectionMonitorThread)

"pool-1-thread-1" #13 prio=5 os_prio=31 tid=0x00007fdcae064800 nid=0x5f03 runnable [0x000000012d0aa000]
   java.lang.Thread.State: RUNNABLE
    at sun.nio.ch.KQueueArrayWrapper.kevent0(Native Method)
    at sun.nio.ch.KQueueArrayWrapper.poll(KQueueArrayWrapper.java:198)
    at sun.nio.ch.KQueueSelectorImpl.doSelect(KQueueSelectorImpl.java:103)
    at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
    - locked <0x000000076bc15568> (a sun.nio.ch.Util$2)
    - locked <0x000000076bc154e8> (a java.util.Collections$UnmodifiableSet)
    - locked <0x000000076bc13bf0> (a sun.nio.ch.KQueueSelectorImpl)
    at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
    at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.execute(AbstractMultiworkerIOReactor.java:340)
    at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.execute(PoolingNHttpClientConnectionManager.java:189)
    at org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase.doExecute(CloseableHttpAsyncClientBase.java:67)
    at org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase.access$000(CloseableHttpAsyncClientBase.java:38)
    at org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase$1.run(CloseableHttpAsyncClientBase.java:57)
    at java.lang.Thread.run(Thread.java:745)

"Thread-1" #12 daemon prio=5 os_prio=31 tid=0x00007fdcae94b800 nid=0x5d03 in Object.wait() [0x000000012cf97000]
   java.lang.Thread.State: TIMED_WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x000000076bbb12f8> (a com.mashape.unirest.http.utils.SyncIdleConnectionMonitorThread)
    at com.mashape.unirest.http.utils.SyncIdleConnectionMonitorThread.run(SyncIdleConnectionMonitorThread.java:22)
    - locked <0x000000076bbb12f8> (a com.mashape.unirest.http.utils.SyncIdleConnectionMonitorThread)

"Monitor Ctrl-Break" #10 daemon prio=5 os_prio=31 tid=0x00007fdcae012000 nid=0x5b03 runnable [0x000000012cc0f000]
   java.lang.Thread.State: RUNNABLE
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:150)
    at java.net.SocketInputStream.read(SocketInputStream.java:121)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
    - locked <0x000000076b026110> (a java.io.InputStreamReader)
    at java.io.InputStreamReader.read(InputStreamReader.java:184)
    at java.io.BufferedReader.fill(BufferedReader.java:161)
    at java.io.BufferedReader.readLine(BufferedReader.java:324)
    - locked <0x000000076b026110> (a java.io.InputStreamReader)
    at java.io.BufferedReader.readLine(BufferedReader.java:389)
    at com.intellij.rt.execution.application.AppMain$1.run(AppMain.java:88)
    at java.lang.Thread.run(Thread.java:745)

"Service Thread" #9 daemon prio=9 os_prio=31 tid=0x00007fdcaf00a000 nid=0x5703 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C1 CompilerThread3" #8 daemon prio=9 os_prio=31 tid=0x00007fdcae800000 nid=0x5503 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread2" #7 daemon prio=9 os_prio=31 tid=0x00007fdcac809000 nid=0x5303 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread1" #6 daemon prio=9 os_prio=31 tid=0x00007fdcab82c800 nid=0x5103 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" #5 daemon prio=9 os_prio=31 tid=0x00007fdcab82b000 nid=0x4f03 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" #4 daemon prio=9 os_prio=31 tid=0x00007fdcab838000 nid=0x4d03 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Finalizer" #3 daemon prio=8 os_prio=31 tid=0x00007fdcab812000 nid=0x3903 in Object.wait() [0x000000012c120000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x000000076ab06280> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:142)
    - locked <0x000000076ab06280> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:158)
    at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)

"Reference Handler" #2 daemon prio=10 os_prio=31 tid=0x00007fdcab05c800 nid=0x3703 in Object.wait() [0x000000012c01d000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x000000076ab05cf0> (a java.lang.ref.Reference$Lock)
    at java.lang.Object.wait(Object.java:502)
    at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:157)
    - locked <0x000000076ab05cf0> (a java.lang.ref.Reference$Lock)

"VM Thread" os_prio=31 tid=0x00007fdcab058000 nid=0x3503 runnable 

"GC task thread#0 (ParallelGC)" os_prio=31 tid=0x00007fdcab80f800 nid=0x2503 runnable 

"GC task thread#1 (ParallelGC)" os_prio=31 tid=0x00007fdcae000000 nid=0x2703 runnable 

"GC task thread#2 (ParallelGC)" os_prio=31 tid=0x00007fdcab810000 nid=0x2903 runnable 

"GC task thread#3 (ParallelGC)" os_prio=31 tid=0x00007fdcab810800 nid=0x2b03 runnable 

"GC task thread#4 (ParallelGC)" os_prio=31 tid=0x00007fdcab811000 nid=0x2d03 runnable 

"GC task thread#5 (ParallelGC)" os_prio=31 tid=0x00007fdcac807800 nid=0x2f03 runnable 

"GC task thread#6 (ParallelGC)" os_prio=31 tid=0x00007fdcae001000 nid=0x3103 runnable 

"GC task thread#7 (ParallelGC)" os_prio=31 tid=0x00007fdcae001800 nid=0x3303 runnable 

"VM Periodic Task Thread" os_prio=31 tid=0x00007fdcab840800 nid=0x5903 waiting on condition 

JNI global references: 283

Heap
 PSYoungGen      total 76288K, used 39323K [0x000000076ab00000, 0x0000000770000000, 0x00000007c0000000)
  eden space 65536K, 60% used [0x000000076ab00000,0x000000076d166e60,0x000000076eb00000)
  from space 10752K, 0% used [0x000000076f580000,0x000000076f580000,0x0000000770000000)
  to   space 10752K, 0% used [0x000000076eb00000,0x000000076eb00000,0x000000076f580000)
 ParOldGen       total 175104K, used 0K [0x00000006c0000000, 0x00000006cab00000, 0x000000076ab00000)
  object space 175104K, 0% used [0x00000006c0000000,0x00000006c0000000,0x00000006cab00000)
 Metaspace       used 9174K, capacity 9400K, committed 9728K, reserved 1058816K
  class space    used 1102K, capacity 1155K, committed 1280K, reserved 1048576K

任何修复此功能的指针都很有用。

2 个答案:

答案 0 :(得分:3)

您需要致电Unirest.shutdown();

documentation`

答案 1 :(得分:1)

我们可以从转储中看到有两个com.mashape.unirest.http.utils.SyncIdleConnectionMonitorThread线程被卡住(可能是死锁)。

您最常使用的库不是释放资源,也不是存在死锁。一个丑陋的解决方法是使用System.exit()作为completed()中的最后一个操作,但我个人更愿意找到另一个(更稳定的)提供相同服务的库。

您遇到的问题也可能是由于您从IDE(IntelliJ)运行代码这一事实。尝试从命令行运行相同的代码,看看是否可以重现。