2 Replies Latest reply: Apr 6, 2014 2:19 PM by user10878887 RSS

    Blocking application

    user10878887

      Helo

       

      I use JDK 1.7.

      My java application is blocking from time to time.

      I have added some log lines and now I know exactly where is blocking, but I have no solution to fix it.

       

      This code runs in a paralel thread, distinct from the main application thread and updates a big JTextArea swing:

      try {
      ....
          JTextArea element = (JTextArea)add_containers[objColIndex];
          System.out.println("setEditValue start seting text ");
          element.setText(VAL);
          System.out.println("setEditValue text is set ");
      ....
      } catch(Exception ex) {
          System.out.println("SOME error "+ex.getMessage());
          Logger.getLogger(GridC.class.getName()).log(Level.SEVERE, null, ex);
          statD.succes = false;
          statD.errMessage = ex.getMessage();
          return statD;
      }
      
      
      

      The code reaches "setEditValue start seting tex" but never reaches "setEditValue text is set". No exception is triggered.

      The whole application is blocked - main thread and this paralel thread.

      This section of code is called many times, but in rare cases is blocking without any apparent reason.

      Sometimes I run the exact same instuctions and is blocking and sometimes is working.

       

      I don't know how to debug this or how to fix it.

      Can someoane help?

       

      Thank you.

        • 1. Re: Blocking application
          baftos

          My guess. Is the thread calling setText(), the Event Disptach Thread(EDT)? If not, make the necessary arrangements. See SwingUtilities.invoke...() methods.

          • 2. Re: Blocking application
            user10878887

            You are right, the setText is not in the EDT thread.

            I fixed the code.

            Thank you, I don't believe I would of fixed this on my own very soon.

             

            This is my new code:

            try {  
            ...  
                if(!javax.swing.SwingUtilities.isEventDispatchThread())  {
                    Runnable doThread = new Runnable() {
                                @Override
                                    public void run() {
                                        JTextArea element = (JTextArea)add_containers[objColIndex];
                                        element.setText(VAL);
                                    }
                                };            
                            SwingUtilities.invokeLater(doThread);
            
                } else {
                        JTextArea element = (JTextArea)add_containers[objColIndex];  
                        element.setText(VAL);  
                }
            ...  
            } catch(Exception ex) {  
                System.out.println("SOME error "+ex.getMessage());  
                    Logger.getLogger(GridC.class.getName()).log(Level.SEVERE, null, ex);  
                    statD.succes = false;  
                    statD.errMessage = ex.getMessage();  
                    return statD;  
            }
            
            

             

            Is this the best way to do it?