简体   繁体   English

Java线程和HTTP请求死锁

[英]Java threads and HTTP requests deadlock

I encounter a problem with java threads and I am not sure if its related to my approach or if thread pooling is going to resolve what I am trying to achieve. 我在Java线程中遇到问题,我不确定它是否与我的方法有关,或者线程池是否可以解决我要实现的问题。

        for (int i = 0; i < 100; i++) {
            verifier[i]=new Thread();
            verifier[i].start();
        }

I initialize 100 threads and start them. 我初始化100个线程并启动它们。 In the threads the code that gets executed is just 在线程中执行的代码只是

        con = (HttpURLConnection) website.openConnection(url);
        // gets only the header
        con.setRequestMethod("HEAD");
        con.setConnectTimeout(2000); // set timeout to 2 seconds

These threads repeat the process above over a long list of url/data. 这些线程在一长串url / data上重复上述过程。

The first 50 threads execute almost instantly then they just stop for 60 seconds or so and then there is another spike of execution that 20 of them or so finish at the same time and so on. 前50个线程几乎立即执行,然后它们仅停止60秒左右,然后又有一个执行高峰,它们中的20个左右同时完成,依此类推。 The same deadlock occurs even if there is 4 of them. 即使有四个,也会发生相同的死锁。

My first guess was a deadlock. 我的第一个猜测是僵局。 I am not sure how to resolve the issue and maintain a constant execution pace, without deadlocks and stops. 我不确定如何解决问题并保持稳定的执行速度,而不会出现死锁和停止。

I am looking for an explanation of why this occurs and how it can be resolved. 我正在寻找一种解释为什么会发生以及如何解决它。

By DeadLock I reefer to the Java Virtual Machine and how it handles thread. 通过DeadLock,我可以回顾Java虚拟机及其如何处理线程。 Not deadlock caused by my threads. 没有死线是由我的线程引起的。

SCREENSHOT OF THREAD EXECUTION: 线程执行屏幕快照:

在此处输入图片说明

It looks like the threads are dying for no reason and I don't know why?! 线程似乎无缘无故地死了,我不知道为什么?!

It could be that the operating system configurable limit of tcp/ip connections gets hit, which causes the JVM to block waiting for a new TCP/IP connection to be created, which will only happen if a connection already used get's closed. 可能是操作系统达到了tcp / ip连接的可配置限制,这导致JVM阻塞等待创建新的TCP / IP连接,只有在关闭已使用的连接时才会发生。

This could help to find what is going on: 这可能有助于查找正在发生的情况:

  • profile the run with visualvm which comes with the JVM itself (run it on the command line with jvisualvm). 轮廓与运行VisualVM的附带JVM本身(与jvisualvm在命令行上运行它)。 There should be indication of how many threads are created and why are they blocked, deadlocks, etc. 应该指出创建了多少个线程以及为什么它们被阻塞,死锁等。

  • Wait for it to block and take thread dumps of the JVM process to check for deadlocks in the thread stack traces using jstack or visualvm, search for the deadlock keyword. 等待它阻塞并进行JVM进程的线程转储,以使用jstack或visualvm检查线程堆栈跟踪中的死锁,并搜索deadlock关键字。

  • Check with netstat -nao the state of your TCP connections, to see if the operating system limit is getting hit, if there are many connections in CLOSE_WAIT at the times the blocking occurs 使用netstat -nao检查TCP连接的状态,以查看是否达到操作系统限制,在发生阻塞时CLOSE_WAIT中是否有许多连接

  • Are you behind a corporate proxy/firewall, you could be hitting some other sort of security limit that prevents you from opening more TCP connections, not necessarily the limit of the operating system 在企业代理/防火墙的背后,您可能会遇到其他类型的安全限制,该限制会阻止您打开更多的TCP连接,而不一定是操作系统的限制

If none of this helps you can always edit the question with further findinds, but based on the description of the code other limits are getting hit that on a first look don't seem related to JVM thread deadlocks, hope this helps. 如果以上方法都不能帮助您始终使用进一步的findinds编辑该问题,但是基于代码的描述,乍看之下似乎与JVM线程死锁无关的其他限制正在受到打击,希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM