狂徒稽太美 2022-06-25 17:51 采纳率: 0%
浏览 62

HttpURLConnection和HttpsURLConnection同时在try运行时线程死锁

问题遇到的现象和发生背景

程序死锁,try在同时运行HttpsURLConnection for释放close()和HttpURLConnection for释放close()时线程假死

问题相关代码,请勿粘贴截图

        Button button = new Button("\u5F00\u59CB\u8F70\u70B8");
        button.addActionListener(new ActionListener() {
            private BufferedReader read;

            public void actionPerformed(ActionEvent e) {
                if(textField.getText().equals("")) {
                    JOptionPane.showMessageDialog(null,"请输入电话号~","错误",JOptionPane.ERROR_MESSAGE);
                }else if(textField_1.getText().equals("")){
                    JOptionPane.showMessageDialog(null,"请输入提交方式~","错误",JOptionPane.ERROR_MESSAGE);
                }else{
                    JOptionPane.showMessageDialog(null,"Determine whether to start bombing","正确",JOptionPane.INFORMATION_MESSAGE);
                    if(textField_1.getText().equals("URL")) {
                        // 确定服务器链接
                        String link = "http://10.220.173.126:8005/application/index.php?op=1&Submission=GET&mtxt=HonZa.txt";
                        //这里可以获取加个编辑框获取
                        int responsecode = 0;
                        HttpURLConnection urlConnection;
                        HttpsURLConnection urlConnection1;
                        BufferedReader reader;
                        URL url;
                        int runTimes = 1;
                        for (int runtime = 0; runtime < runTimes; runtime++) {

                        try{

                        url=new URL(link);
                                //打开URL
                                urlConnection = (HttpURLConnection)url.openConnection();
                                //获取服务器响应代码
                                responsecode=urlConnection.getResponseCode();

                                String line;
                                if(responsecode==200){
                                    //得到输入流,即获得了网页的内容 
                                    reader=new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"utf-8"));

                                    // 新建一个容器,容器的长度会随内容的增加而增加,不用担心数组越界的问题

                                            ArrayList al = new ArrayList();
                                    while((line=reader.readLine())!=null){
                                        al.add(line);
                                    }
                                    reader.close();
                            // 关闭流
                            String[] str = new String[al.size()];
                            for (int i = 0; i < str.length; i++) {
                                str[i] = String.valueOf(al.get(i));
                            }
                            StringBuffer sb = new StringBuffer();
                            for (int i1 = 0;i1<str.length;i1++){
                        try {
                              //6.打印输出解码后的字符串
                            if (str[i1].contains("http://")){
                        BufferedReader reader1;
                        try {

                            String hzur4l=str[i1].replace("[手机号码]", textField.getText());
                                url=new URL(hzur4l);
                                        //打开URL
                                        urlConnection = (HttpURLConnection)url.openConnection();
                                        //获取服务器响应代码
                                        responsecode=urlConnection.getResponseCode();

                                        String line1;
                                        if(responsecode==200){
                                            //得到输入流,即获得了网页的内容 
                                            reader1=new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"utf-8"));

                                            // 新建一个容器,容器的长度会随内容的增加而增加,不用担心数组越界的问题

                                            while((line1=reader1.readLine())!=null){
                                                textArea.setText(textArea.getText()+hzur4l+"\n接口http访问完毕,返回状态码:"+responsecode+"\n");
                                            }
                                            reader1.close();
                                        }
                        }catch(Exception e1){
                            textArea.setText(textArea.getText()+"http访问手机号,出现异常:"+e1);
                            }
                        }else if(str[i1].contains("https://")){

                        BufferedReader reader1;

                        try {
                            String hzurl=str[i1].replace("[手机号码]", textField.getText());
                                url=new URL(hzurl); 
                                        //打开URL
                                        urlConnection1 = (HttpsURLConnection)url.openConnection();
                                        //获取服务器响应代码
                                        responsecode=urlConnection1.getResponseCode();

                                        String line1;
                                        if(responsecode==200){
                                            //得到输入流,即获得了网页的内容 
                                            reader1=new BufferedReader(new InputStreamReader(urlConnection1.getInputStream(),"utf-8"));

                                            // 新建一个容器,容器的长度会随内容的增加而增加,不用担心数组越界的问题

                                            while((line1=reader1.readLine())!=null){
                                                textArea.setText(textArea.getText()+hzurl+"\n接口https访问完毕,返回状态码:"+responsecode+"\n");
                                            }
                                            reader1.close();
                                        }
                        }catch(Exception e2){
                            textArea.setText(textArea.getText()+"https访问手机号,出现异常:"+e2);
                            }

                        }
                        }catch(Exception e3){
                            textArea.setText(textArea.getText()+"设置手机号,出现异常:"+e3);
                            }

                        //重新开始 runTimes++ 达成死循环;
                        }
                            }
                        }catch(Exception e4){
                            textArea.setText(textArea.getText()+"获取不到网页的源码,出现异常:"+e4);
                            }
                        }
                    }else if(textField_1.getText().equals("Web")){
                        int responsecode = 0;
                        HttpURLConnection urlConnection;
                        HttpsURLConnection urlConnection1;
                        URL url;
                        int runTimes = 1;
                        for (int runtime = 0; runtime < runTimes; runtime++) {

                        try{

                            String temp;
                            File f = new File("res/HonZa.txt");
                            String adn="";
                           //指定读取编码用于读取中文
                            read=new BufferedReader(new InputStreamReader(new FileInputStream(f),"utf-8"));
                            ArrayList al3 = new ArrayList();
                            BufferedReader reader=new BufferedReader(read); 
                            //bufReader = new BufferedReader(new FileReader(filepath));

                            while((temp=read.readLine())!=null){
                                al3.add(temp);
                            }
                            read.close();

                            String[] str1 = new String[al3.size()];
                            for (int i = 0; i < str1.length; i++) {
                                str1[i] = String.valueOf(al3.get(i));
                            }

                            StringBuffer sb1 = new StringBuffer();
                            for (int i1 = 0;i1<str1.length;i1++){

                                try {
                                      //6.打印输出解码后的字符串
                                    if (str1[i1].contains("http://")){
                                BufferedReader reader1;
                                try {
                                    String hzurl=str1[i1].replace("[手机号码]", textField.getText());
                                        url=new URL(hzurl);
                                                //打开URL
                                                urlConnection = (HttpURLConnection)url.openConnection();
                                                //获取服务器响应代码
                                                responsecode=urlConnection.getResponseCode();

                                                String line1;
                                                if(responsecode==200){
                                                    //得到输入流,即获得了网页的内容 
                                                    reader1=new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"utf-8"));

                                                    // 新建一个容器,容器的长度会随内容的增加而增加,不用担心数组越界的问题

                                                    while((line1=reader1.readLine())!=null){
                                                        textArea.setText(textArea.getText()+hzurl+"\n接口http访问完毕,返回状态码:"+responsecode+"\n");
                                                    }
                                                    reader1.close();
                                                }
                                }catch(Exception e1){
                                    textArea.setText(textArea.getText()+"http访问手机号,出现异常:"+e1);
                                    }
                                }else if(str1[i1].contains("https://")){

                                BufferedReader reader2;

                                try {
                                    String hzurl=str1[i1].replace("[手机号码]", textField.getText());
                                        url=new URL(hzurl); 
                                                //打开URL
                                                urlConnection1 = (HttpsURLConnection)url.openConnection();
                                                //获取服务器响应代码
                                                responsecode=urlConnection1.getResponseCode();

                                                String line1;
                                                if(responsecode==200){
                                                    //得到输入流,即获得了网页的内容 
                                                    reader2=new BufferedReader(new InputStreamReader(urlConnection1.getInputStream(),"utf-8"));

                                                    System.out.println("url连接成功!\n");
                                                    // 新建一个容器,容器的长度会随内容的增加而增加,不用担心数组越界的问题

                                                    while((line1=reader2.readLine())!=null){
                                                        textArea.setText(textArea.getText()+hzurl+"\n接口https访问完毕,返回状态码:"+responsecode+"\n");
                                                    }
                                                    reader2.close();
                                                }
                                }catch(Exception e2){
                                    textArea.setText(textArea.getText()+"https访问手机号,出现异常:"+e2);
                                    System.out.println("https访问手机号,出现异常:"+e2);
                                    }

                                }
                                }catch(Exception e3){
                                    textArea.setText(textArea.getText()+"设置手机号,出现异常:"+e3);
                                    System.out.println("设置手机号,出现异常:"+e3);
                                    }

                            }

                            
                            }catch (Exception e5) {
                                textArea.setText(textArea.getText()+"设置手机号,出现异常:"+e5);
                                System.out.println(e5);
                           // TODO: handle exception
                            }    
                        }
                    }
                }
            }
        });

运行结果及报错内容

正常运行不会异常报错,但HttpsURLConnection和HttpURLConnectionfor循环全部完成后不会结束循环

Full thread dump OpenJDK 64-Bit Server VM (25.332-b08 mixed mode):

"Keep-Alive-Timer" #28 daemon prio=8 os_prio=1 tid=0x00000000275b0000 nid=0x3974 waiting on condition [0x000000001fd1f000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep(Native Method)
        at sun.net.www.http.KeepAliveCache.run(KeepAliveCache.java:172)
        at java.lang.Thread.run(Thread.java:750)

"D3D Screen Updater" #19 daemon prio=7 os_prio=1 tid=0x000000001f3f4800 nid=0x2e18 in Object.wait() [0x000000002921f000]
   java.lang.Thread.State: TIMED_WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        at sun.java2d.d3d.D3DScreenUpdateManager.run(D3DScreenUpdateManager.java:423)
        - locked <0x000000076fa00768> (a java.lang.Object)
        at java.lang.Thread.run(Thread.java:750)

"TimerQueue" #17 daemon prio=5 os_prio=0 tid=0x000000001f18a000 nid=0x1614 waiting on condition [0x000000002660f000]
   java.lang.Thread.State: WAITING (parking)
        at sun.misc.Unsafe.park(Native Method)
        - parking to wait for  <0x000000076fa18188> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
        at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
        at java.util.concurrent.DelayQueue.take(DelayQueue.java:211)
        at javax.swing.TimerQueue.run(TimerQueue.java:174)
        at java.lang.Thread.run(Thread.java:750)

"DestroyJavaVM" #16 prio=5 os_prio=0 tid=0x0000000000435000 nid=0x6df4 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"AWT-EventQueue-0" #15 prio=6 os_prio=0 tid=0x000000001eb80000 nid=0x194c runnable [0x000000001fc1c000]
   java.lang.Thread.State: RUNNABLE
        at java.net.DualStackPlainSocketImpl.connect0(Native Method)
        at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
        - locked <0x000000076b05d668> (a java.net.DualStackPlainSocketImpl)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:607)
        at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:293)
        at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:173)
        at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
        - locked <0x000000076b05d4c8> (a sun.net.www.protocol.https.HttpsClient)
        at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:264)
        at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:367)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:203)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1162)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1056)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:189)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1572)
        - locked <0x000000076b05c9d0> (a sun.net.www.protocol.https.DelegateHttpsURLConnection)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1500)
        - locked <0x000000076b05c9d0> (a sun.net.www.protocol.https.DelegateHttpsURLConnection)
        at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:352)
        at com.gui.iop.SMS_bombing$2.actionPerformed(SMS_bombing.java:339)
        at java.awt.Button.processActionEvent(Button.java:409)
        at java.awt.Button.processEvent(Button.java:377)
        at java.awt.Component.dispatchEventImpl(Component.java:4889)
        at java.awt.Component.dispatchEvent(Component.java:4711)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
        at java.awt.EventQueue.access$500(EventQueue.java:97)
        at java.awt.EventQueue$3.run(EventQueue.java:709)
        at java.awt.EventQueue$3.run(EventQueue.java:703)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
        at java.awt.EventQueue$4.run(EventQueue.java:733)
        at java.awt.EventQueue$4.run(EventQueue.java:731)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

"AWT-Windows" #13 daemon prio=6 os_prio=0 tid=0x000000001eb73000 nid=0x2760 runnable [0x000000001f93f000]
   java.lang.Thread.State: RUNNABLE
        at sun.awt.windows.WToolkit.eventLoop(Native Method)
        at sun.awt.windows.WToolkit.run(WToolkit.java:316)
        at java.lang.Thread.run(Thread.java:750)

"AWT-Shutdown" #12 prio=5 os_prio=0 tid=0x000000001eb64800 nid=0x6f68 in Object.wait() [0x000000001f83f000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x000000076fa103b8> (a java.lang.Object)
        at java.lang.Object.wait(Object.java:502)
        at sun.awt.AWTAutoShutdown.run(AWTAutoShutdown.java:295)
        - locked <0x000000076fa103b8> (a java.lang.Object)
        at java.lang.Thread.run(Thread.java:750)

"Java2D Disposer" #11 daemon prio=10 os_prio=2 tid=0x000000001eb64000 nid=0x6f8c in Object.wait() [0x000000001f73f000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
        - locked <0x000000076fa28178> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
        at sun.java2d.Disposer.run(Disposer.java:148)
        at java.lang.Thread.run(Thread.java:750)

"Service Thread" #10 daemon prio=9 os_prio=0 tid=0x000000001cbfc800 nid=0x2054 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C1 CompilerThread3" #9 daemon prio=9 os_prio=2 tid=0x000000001cbfc000 nid=0x44e8 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread2" #8 daemon prio=9 os_prio=2 tid=0x000000001cbfb000 nid=0x2684 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread1" #7 daemon prio=9 os_prio=2 tid=0x000000001cbf0800 nid=0x627c waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" #6 daemon prio=9 os_prio=2 tid=0x000000001cbf5000 nid=0x2b88 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000000001cbd3000 nid=0xec4 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001cbd0800 nid=0x157c runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000000001bf3d000 nid=0x236c in Object.wait() [0x000000001e11f000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
        - locked <0x000000076fa18388> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
        at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)

"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x000000001cb49000 nid=0x29c4 in Object.wait() [0x000000001e01f000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        at java.lang.Object.wait(Object.java:502)
        at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
        - locked <0x000000076fa202f0> (a java.lang.ref.Reference$Lock)
        at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)

"VM Thread" os_prio=2 tid=0x000000001cb23000 nid=0x4184 runnable

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x000000000044c800 nid=0x41d0 runnable

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x000000000044e000 nid=0x19dc runnable

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x0000000000450000 nid=0x4714 runnable

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x0000000000451800 nid=0x4c18 runnable

"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x0000000000454000 nid=0x566c runnable

"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x0000000000455800 nid=0x65bc runnable

"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x0000000000459000 nid=0x583c runnable

"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x000000000045a000 nid=0x5cf0 runnable

"VM Periodic Task Thread" os_prio=2 tid=0x000000001cbfe800 nid=0x5bb8 waiting on condition

JNI global references: 383

我的解答思路和尝试过的方法

使用run 主线程进行耗时任务,子线程进行.setText更新

我想要达到的结果

优化try过多自动睡眠和for循环死锁 导致可视化UI和jvm无响应只能在eclipse终止和去除启动

  • 写回答

1条回答 默认 最新

  • a1767028198 2022-06-26 09:22
    关注

    com.gui.iop.SMS_bombing$2.actionPerformed(SMS_bombing.java:339) 这个类这里写的啥

    评论

报告相同问题?

问题事件

  • 创建了问题 6月25日

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题