0

I am getting the following amazing exception when I am using multi-threading in my code but, I can not figure out where it occurs. Also this exception occurs only sometimes.

Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException: Vector Enumeration at java.util.Vector$1.nextElement(Vector.java:348) at javax.swing.plaf.basic.BasicTableHeaderUI.getPreferredSize(BasicTableHeaderUI.java:793) at javax.swing.JComponent.getPreferredSize(JComponent.java:1660)

I am using the below code for multi-threading in my application.

       try {
        Thread Thread4 = new Thread() {
           public void run() {
                GetOrderData(mID, "OrderInfo_Orn");
            }
        };
        Thread4.start();
        Thread4.sleep(20);

        Thread Thread5 = new Thread() {
           public void run() {
                GetOrderData(mID, "OrderInfo_Parts");
            }
        };
        Thread5.start();
        Thread5.sleep(20);
        Thread queryThread = new Thread() {

            public void run() {
                GetMasterData(mID, rowId);
            }
        };
        queryThread.start();
        queryThread.sleep(20);
        Thread Thread2 = new Thread() {
            public void run() {
                GetDetailData(mID, "'RcvPrePolishGoods_Detail'");
            }
        };
        Thread2.start();
        Thread2.sleep(20);

        Thread Thread3 = new Thread() {
            public void run() {
                GetDetailData(mID, "'RcvPrePolishGoods_Parts'");
            }
        };
        Thread3.start();
        Thread3.sleep(20);
      } catch (InterruptedException ex) {
        Logger.getLogger(RcvPrePolishGoods.class.getName()).log(Level.SEVERE, null, ex);
    }
slava
  • 1,901
  • 6
  • 28
  • 32
  • Not related to your problem, but I strongly recommend to use an Executer instead of working with Threads directly. Have a look at `java.concurrent.Executors` – mbelow Nov 27 '12 at 09:36
  • The exceptions have dramatically reduced with using simple threading instead of SwqingWorker.execute – Syed Muhammad Mubashir Nov 27 '12 at 09:37
  • 3
    Also note that it is only legal to access your Swing GUI from within the GUI thread - but this should generally throw a more qualified exception. – mbelow Nov 27 '12 at 09:38
  • 3
    `Thread3.sleep(20);` sleep is a static method, it should therefore be accessed in a static manner: `Thread.sleep(20)`! Also, start following Java naming conventions: methods and variables start with a lower case letter. – Guillaume Polet Nov 27 '12 at 09:41
  • @Syed Muhammad Mubashir hmmmm again I'll complicating simple things, idea by (@user1547018) should be correct, you can to invoke [SwingWoker, Thread or Runnable from Executor, notice Executor is designed to starting threads, not to listening or returns stutuses, simple there isn't interactions](http://stackoverflow.com/a/6186188/714968) – mKorbel Nov 27 '12 at 09:42
  • 4
    @user1547018 Actually, no. The result of violating the single Thread access to Swing can only result in unpredictable and ununderstandable errors as the one described above. – Guillaume Polet Nov 27 '12 at 09:42
  • 5
    See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html). You are not allowed to make changes to Swing components on other threads then the EDT. Looking at the stack trace you are modyfing your table data on the wrong thread – Robin Nov 27 '12 at 09:44

1 Answers1

0

Theres a couple things you want to look into:

  1. If your updating the GUI (adding items to a JTable is what the exception implies) from the Get****Data functions, you're going to run into EDT problems (that's the AWT-EventQueue-0 you see in the error). Instead use SwingUtils.InvokeLater(). Or if you need it to run after 20 ms (or at 20 ms intervals), use a Swing Timer

  2. You're calling threadname.start(); and then in the next line, calling threadname.sleep(20);. According to the API, Thread.sleep causes the currently running thread to wait - which in this case is the EDT (not threadname). If you want threadname to sleep, place the sleep inside the runnable like this:

    Thread Thread5 = new Thread() {
       public void run() {
            Thread.sleep();
            GetOrderData(mID, "OrderInfo_Parts");
        }
    };
    
Nick Rippe
  • 6,465
  • 14
  • 30
  • hehe... sorry for the copy/paste. – Nick Rippe Nov 27 '12 at 15:22
  • @NickRippe This solution not working in my case as it is freezing my GUi even worst than before using Threads – Syed Muhammad Mubashir Nov 28 '12 at 02:17
  • Also second solution is not useful as also cause many exceptions – Syed Muhammad Mubashir Nov 28 '12 at 02:38
  • @SyedMuhammadMubashir Yes - executing code on the EDT does freeze your GUI - Any code executing on the EDT will block GUI updates (causing the freezing you're referring to). So if your `Get` methods are long running you don't want to run them on the EDT. That means you'll use SwingUtils.InvokeLater() to execute whatever is updating the JTable (inside of your `Get` functions). Basically you need to combine 1 & 2 in order to create your answer. Also, while you're free to downvote my answers, you're more likely to get my help if your nice. – Nick Rippe Nov 28 '12 at 14:40