Java 当一个方法调用仍在进行时调用第二个方法会导致程序崩溃

Java 当一个方法调用仍在进行时调用第二个方法会导致程序崩溃,java,methods,crash,Java,Methods,Crash,我的程序中有两个方法有点问题:我有一个方法,当用户单击GUI上的开始按钮时调用。此方法将接收当前通过网络发送/接收的所有网络流量,并向用户显示相关信息。用户可以通过单击停止按钮停止调用此方法。 此方法当前看起来如下所示: public static void retrieveFilteredPdu(){ /*Try/Catch block copied from 'receivePdu()' method in EspduReceiver.java on 07/05/2014

我的程序中有两个方法有点问题:我有一个方法,当用户单击GUI上的开始按钮时调用。此方法将接收当前通过网络发送/接收的所有网络流量,并向用户显示相关信息。用户可以通过单击停止按钮停止调用此方法。 此方法当前看起来如下所示:

public static void retrieveFilteredPdu(){

    /*Try/Catch block copied from 'receivePdu()' method in EspduReceiver.java on 07/05/2014
     * Now edit it so that it will receive all PDUs, but only display the ones matching the filter values in the top JTextArea */
    try{
        /*Specify the socket to receive the data */
        EspduReceiver.socket = new MulticastSocket(EspduSender.PORT);
        EspduReceiver.address = InetAddress.getByName(EspduSender.DEFAULT_MULTICAST_GROUP);
        EspduReceiver.socket.joinGroup(EspduReceiver.address); 

        //stopCapture = false;

        /*Loop infinitely, receiving datagrams */
        while(true){
            /*First, set the value of the 'stopCapture' boolean to 'false', in case it has previously been set to true during the life of
             * the program */
            /*stopCapture = false; /*For some reason, if I do this here, clicking the 'Get site' button causes the program to start receiving
            PDUs again, and you are not able to stop it without manually shutting down the program. Possibly because I am infinitely
            setting the value of 'stopCapture' to false?*/

            byte buffer[] = new byte[EspduReceiver.MAX_PDU_SIZE];
            EspduReceiver.packet = new DatagramPacket(buffer, buffer.length);

            EspduReceiver.socket.receive(EspduReceiver.packet);

            Pdu pdu = EspduReceiver.pduFactory.createPdu(EspduReceiver.packet.getData()); /*Moved this line to the top of the class to declare as global variable (29/04/2014) */

            if(pdu != null){
                System.out.print("Got PDU of type: " + pdu.getClass().getName());
                if(pdu instanceof EntityStatePdu){
                    EntityID eid = ((EntityStatePdu)pdu).getEntityID(); 
                    Vector3Double position = ((EntityStatePdu)pdu).getEntityLocation(); 
                    System.out.println(" EID:[" + eid.getSite() + ", " + eid.getApplication() + ", " + eid.getEntity() + "] ");
                    System.out.println("Location in DIS coordinates: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "] ");
                    /*Add PDU to ArrayList of PDUs */
                    EspduReceiver.espdu.add(pdu);
    /*              System.out.println(" PDU added to arrayList. "); 
                    System.out.println(espdu); /*This is printing out the actual DIS messages (i.e. edu.nps.moves.dis.EntityState...),
                    maybe try adding the 'eid.getSite()', etc to an ArrayList instead. Use Associative arrays/ map/ hashmap */

                    EspduReceiver.entitySite.add(eid.getSite());
                    System.out.println("Entity Site added to ArrayList from Filter.retrieveFilteredPdu() ");
                    EspduReceiver.entityApplication.add(eid.getApplication());
                    System.out.println("Entity Application added to ArrayLIst. ");
                    EspduReceiver.entity.add(eid.getEntity());
                    System.out.println("Entity ID added to ArrayList");

                    /*Check that everything is actually in the ArrayLists 
                    for(int i : entity){ /*Substituted 'entity' with 'entitySite' and 'entityApplication'- values are all printed correctly. 
                    System.out.println(i);
                    } */

                    /*07/05/2014
                     * Write a method that will only append the PDUs that match the filter values to the text area,
                     * call that method here. */



                    /*Now append each PDU to the text area */
                    Gui.displayFilteredOutput.append("\n");
                    Gui.displayFilteredOutput.append("EID: [" + eid.getSite() + ", " + eid.getApplication() + ", " + eid.getEntity() + "]. ");
                    Gui.displayFilteredOutput.append("Location in DIS coordinates: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "] ");

                    /*Append every PDU that matches the filter criteria to the displayFilteredOutput JTextArea 
                    Gui.displayFilteredOutput.append("\n");
                    Gui.displayFilteredOutput.append("EID: [" + eid.getSite() + ", " + eid.getApplication() + ", " + eid.getEntity() + "]. ");
                    Gui.displayFilteredOutput.append("Location in DIS coordinates: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "] "); */


                } else if(!(pdu instanceof EntityStatePdu)){
                    System.out.println("There are no PDUs currently being received.");
                }
                System.out.println();
            }
            Thread.sleep(1000);
            /*Try adding a boolean to allow me to stop the capture by clicking 'stop' button- Look on stackoverflow */
            boolean queryStopCapture =  EspduReceiver.stopCapture;
            if(queryStopCapture ==  true){
                System.out.println("Break clause in 'queryStopCapture' if statement in EspduReceiver.java has been called. ");
                break; /*Use a call to receivePdu() to populate the second JTextArea, but don't let it call a 'break' clause at all. 
             *          Find some other way of adding a 'break' to the output displayed in the first JTextArea (01/05/2014)
             *          Maybe add this code to receivePdu() call in ActionListener instead of here.
             */
            } 
        } /*end while */
    } /*end try */
    catch(Exception e){
        System.out.println(e);
        e.printStackTrace();
        System.out.println("Error in retrieveFilteredPdu() method. ");
        /*09/04/2014 @ 17:100
         * If this exception gets called, presumably it either means that pdu is not an instance of EntityStatePdu, or
         * that pdu does not actually hold a packet.  */
    }

}
public static void filterPDUs(){
//  if(EspduReceiver.startCapture == true){
        /*If EspduReceiver.startCapture is true, then the program is already receiving PDUs, so now I need to set it to only display the ones that
         * match the filter criteria. Do this by checking the PDU attributes against the filter values before printing- if they match, then print,
         * if not, don't. */
        if((EspduReceiver.startCapture == true) && (EspduReceiver.stopCapture == false)){
            /*Get the size of the sitesToBeFiltered ArrayList, and store in a variable. Will need to update the variable with every iteration
             * of the loop, because the ArrayList will keep growing as long as the capture is running. */
            int sitesToBeFilteredSize = sitesToBeFiltered.size();
            int matchingSitesIterator = 0;
            /*First, check if site filter value matches the PDU's site, if it does, then check Application, if it matches again, then check ID.
             * If at any point it doesn't match, exit the while loop. */
            while(matchingSitesIterator < sitesToBeFilteredSize){
                System.out.println("SitesToBeFiltered.size() = " + sitesToBeFilteredSize);
                if(sitesToBeFiltered.get(matchingSitesIterator) == Filter.filter1Value){
                    if(applicationsToBeFiltered.get(matchingSitesIterator) == Filter.filter2Value){
                        if(IDsToBeFiltered.get(matchingSitesIterator) == Filter.filter3Value){
                            Gui.displayFilteredOutput.append("Matching PDU found: [" + sitesToBeFiltered.get(matchingSitesIterator) + ", " + applicationsToBeFiltered.get(matchingSitesIterator) + ", " + IDsToBeFiltered.get(matchingSitesIterator) + "] ");
                        } else {Gui.displayFilteredOutput.append("Sorry, there were no PDUs found with the specified ID value.");}
                        Gui.displayFilteredOutput.append("Need to display every PDU that had a matching Site & Application here. ");
                    }else {Gui.displayFilteredOutput.append("Sorry, there were no PDUs found with the specified Application value.");}
                    Gui.displayFilteredOutput.append("need to display every PDU that had a matching Site here. ");
                }else {Gui.displayOutput.append("Sorry, there were no PDUs found with the specified Site value.");}
            }
        } else {
            Gui.displayFilteredOutput.append("Do something if EspduReceiver.startCapture is not true, and EspduReceiver.stopCapture is not false");
        }

//  }else if(EspduReceiver.startCapture == false){
        /*If EspduReceiver.startCapture is false, then the program is not receiving PDUs, so I now need to call the method that will receive PDUs, 
         * and display only the ones that match the filter criteria. */
//  }
}
我有另一种方法,理论上,它应该只显示有关网络流量的信息,这些网络流量的属性与用户设置的某些值相匹配。当用户单击GUI上的“过滤器”按钮时,将调用此方法。该方法当前看起来如下所示:

public static void retrieveFilteredPdu(){

    /*Try/Catch block copied from 'receivePdu()' method in EspduReceiver.java on 07/05/2014
     * Now edit it so that it will receive all PDUs, but only display the ones matching the filter values in the top JTextArea */
    try{
        /*Specify the socket to receive the data */
        EspduReceiver.socket = new MulticastSocket(EspduSender.PORT);
        EspduReceiver.address = InetAddress.getByName(EspduSender.DEFAULT_MULTICAST_GROUP);
        EspduReceiver.socket.joinGroup(EspduReceiver.address); 

        //stopCapture = false;

        /*Loop infinitely, receiving datagrams */
        while(true){
            /*First, set the value of the 'stopCapture' boolean to 'false', in case it has previously been set to true during the life of
             * the program */
            /*stopCapture = false; /*For some reason, if I do this here, clicking the 'Get site' button causes the program to start receiving
            PDUs again, and you are not able to stop it without manually shutting down the program. Possibly because I am infinitely
            setting the value of 'stopCapture' to false?*/

            byte buffer[] = new byte[EspduReceiver.MAX_PDU_SIZE];
            EspduReceiver.packet = new DatagramPacket(buffer, buffer.length);

            EspduReceiver.socket.receive(EspduReceiver.packet);

            Pdu pdu = EspduReceiver.pduFactory.createPdu(EspduReceiver.packet.getData()); /*Moved this line to the top of the class to declare as global variable (29/04/2014) */

            if(pdu != null){
                System.out.print("Got PDU of type: " + pdu.getClass().getName());
                if(pdu instanceof EntityStatePdu){
                    EntityID eid = ((EntityStatePdu)pdu).getEntityID(); 
                    Vector3Double position = ((EntityStatePdu)pdu).getEntityLocation(); 
                    System.out.println(" EID:[" + eid.getSite() + ", " + eid.getApplication() + ", " + eid.getEntity() + "] ");
                    System.out.println("Location in DIS coordinates: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "] ");
                    /*Add PDU to ArrayList of PDUs */
                    EspduReceiver.espdu.add(pdu);
    /*              System.out.println(" PDU added to arrayList. "); 
                    System.out.println(espdu); /*This is printing out the actual DIS messages (i.e. edu.nps.moves.dis.EntityState...),
                    maybe try adding the 'eid.getSite()', etc to an ArrayList instead. Use Associative arrays/ map/ hashmap */

                    EspduReceiver.entitySite.add(eid.getSite());
                    System.out.println("Entity Site added to ArrayList from Filter.retrieveFilteredPdu() ");
                    EspduReceiver.entityApplication.add(eid.getApplication());
                    System.out.println("Entity Application added to ArrayLIst. ");
                    EspduReceiver.entity.add(eid.getEntity());
                    System.out.println("Entity ID added to ArrayList");

                    /*Check that everything is actually in the ArrayLists 
                    for(int i : entity){ /*Substituted 'entity' with 'entitySite' and 'entityApplication'- values are all printed correctly. 
                    System.out.println(i);
                    } */

                    /*07/05/2014
                     * Write a method that will only append the PDUs that match the filter values to the text area,
                     * call that method here. */



                    /*Now append each PDU to the text area */
                    Gui.displayFilteredOutput.append("\n");
                    Gui.displayFilteredOutput.append("EID: [" + eid.getSite() + ", " + eid.getApplication() + ", " + eid.getEntity() + "]. ");
                    Gui.displayFilteredOutput.append("Location in DIS coordinates: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "] ");

                    /*Append every PDU that matches the filter criteria to the displayFilteredOutput JTextArea 
                    Gui.displayFilteredOutput.append("\n");
                    Gui.displayFilteredOutput.append("EID: [" + eid.getSite() + ", " + eid.getApplication() + ", " + eid.getEntity() + "]. ");
                    Gui.displayFilteredOutput.append("Location in DIS coordinates: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "] "); */


                } else if(!(pdu instanceof EntityStatePdu)){
                    System.out.println("There are no PDUs currently being received.");
                }
                System.out.println();
            }
            Thread.sleep(1000);
            /*Try adding a boolean to allow me to stop the capture by clicking 'stop' button- Look on stackoverflow */
            boolean queryStopCapture =  EspduReceiver.stopCapture;
            if(queryStopCapture ==  true){
                System.out.println("Break clause in 'queryStopCapture' if statement in EspduReceiver.java has been called. ");
                break; /*Use a call to receivePdu() to populate the second JTextArea, but don't let it call a 'break' clause at all. 
             *          Find some other way of adding a 'break' to the output displayed in the first JTextArea (01/05/2014)
             *          Maybe add this code to receivePdu() call in ActionListener instead of here.
             */
            } 
        } /*end while */
    } /*end try */
    catch(Exception e){
        System.out.println(e);
        e.printStackTrace();
        System.out.println("Error in retrieveFilteredPdu() method. ");
        /*09/04/2014 @ 17:100
         * If this exception gets called, presumably it either means that pdu is not an instance of EntityStatePdu, or
         * that pdu does not actually hold a packet.  */
    }

}
public static void filterPDUs(){
//  if(EspduReceiver.startCapture == true){
        /*If EspduReceiver.startCapture is true, then the program is already receiving PDUs, so now I need to set it to only display the ones that
         * match the filter criteria. Do this by checking the PDU attributes against the filter values before printing- if they match, then print,
         * if not, don't. */
        if((EspduReceiver.startCapture == true) && (EspduReceiver.stopCapture == false)){
            /*Get the size of the sitesToBeFiltered ArrayList, and store in a variable. Will need to update the variable with every iteration
             * of the loop, because the ArrayList will keep growing as long as the capture is running. */
            int sitesToBeFilteredSize = sitesToBeFiltered.size();
            int matchingSitesIterator = 0;
            /*First, check if site filter value matches the PDU's site, if it does, then check Application, if it matches again, then check ID.
             * If at any point it doesn't match, exit the while loop. */
            while(matchingSitesIterator < sitesToBeFilteredSize){
                System.out.println("SitesToBeFiltered.size() = " + sitesToBeFilteredSize);
                if(sitesToBeFiltered.get(matchingSitesIterator) == Filter.filter1Value){
                    if(applicationsToBeFiltered.get(matchingSitesIterator) == Filter.filter2Value){
                        if(IDsToBeFiltered.get(matchingSitesIterator) == Filter.filter3Value){
                            Gui.displayFilteredOutput.append("Matching PDU found: [" + sitesToBeFiltered.get(matchingSitesIterator) + ", " + applicationsToBeFiltered.get(matchingSitesIterator) + ", " + IDsToBeFiltered.get(matchingSitesIterator) + "] ");
                        } else {Gui.displayFilteredOutput.append("Sorry, there were no PDUs found with the specified ID value.");}
                        Gui.displayFilteredOutput.append("Need to display every PDU that had a matching Site & Application here. ");
                    }else {Gui.displayFilteredOutput.append("Sorry, there were no PDUs found with the specified Application value.");}
                    Gui.displayFilteredOutput.append("need to display every PDU that had a matching Site here. ");
                }else {Gui.displayOutput.append("Sorry, there were no PDUs found with the specified Site value.");}
            }
        } else {
            Gui.displayFilteredOutput.append("Do something if EspduReceiver.startCapture is not true, and EspduReceiver.stopCapture is not false");
        }

//  }else if(EspduReceiver.startCapture == false){
        /*If EspduReceiver.startCapture is false, then the program is not receiving PDUs, so I now need to call the method that will receive PDUs, 
         * and display only the ones that match the filter criteria. */
//  }
}

我假设Gui是基于Swing构建的。阅读Swing教程,并注意事件分派线程。我看不出您代码中的Swing部分是如何工作的,但我猜您一定是在某种程度上锁定了EDT。长时间运行的进程需要在自己的线程中运行,并使用SwingUtilities.invokeLater来更新Swing组件,如您的文本区域。

正如其他人所说,您需要显示更多代码,但如果您使用Swingworkers或其他线程来运行这些方法,然后,要么这些方法必须是线程安全的,要么它们需要同步以防止它们的执行被交错。如果您在事件调度线程之外直接访问Swing组件,那么Swing会让恶魔从您的鼻子里冒出来,所以不要这样做,因为有些文本组件有一些特殊的例外,但通常是禁止的。如果您不确定,请将assert java.awt.EventQueue.isDispatchThread放在构造或使用Swing对象的每个语句之前,并在运行时系统中启用断言。

您知道您使用的是静态方法,对吗?这项工作是在单独的线程中完成的吗?荡秋千的人?在UI事件线程上?我在主方法中调用了SwingUtilities.invokeLater-我是否应该以某种方式将长时间运行的方法传递给这个invokeLater方法?我已经将主方法的代码添加到我原来的帖子中。使用synchronized似乎已经解决了这个问题。非常感谢!