-1
    public lyridisplay (java.awt.Frame parent, boolean modal) {
    super(parent, modal); 

    initComponents();//to create a JList

   /* folowing code inside  try preforms DB operations*/
   /*It will return array of  string s*/
       try {
            s = insert.select();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(lyridisplay.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(lyridisplay.class.getName()).log(Level.SEVERE, null, ex);
        }

    //now set the string s to JList

      jList1.setModel(new javax.swing.AbstractListModel() {
        String[] strings =s;
        public int getSize() { return strings.length; }
        public Object getElementAt(int i) { return strings[i]; }
    });

     }

I think the above code should block the EDT , because of the DB operations before setting up JList and it runs on EDT.But It dosn't.The program runs smooth.I did similar thing before,resulting to blocked EDT and a frozen program.I preformed that code in a separate thread,as adviced by SO users.Why this code dosn't block EDT?

Zoyd
  • 3,449
  • 1
  • 18
  • 27
joey rohan
  • 3,505
  • 5
  • 33
  • 70
  • Indeed, it should block the EDT, but maybe that the select statement is too quick to feel any blocked status for the UI... – ioan Jan 16 '13 at 12:52
  • @ioan is the time taken by a select stmt. and insert stmt. same?Cause here i have 5-6 rows in result set,where as last time only one insert blocked my EDT – joey rohan Jan 16 '13 at 12:58
  • Why should it block? If `insert.select()` is quick enough then it won't block. – Mikita Belahlazau Jan 16 '13 at 13:07
  • @NikitaBeloglazov last time one insert is blocking the EDT.But select gives many rows to result set...so..? – joey rohan Jan 16 '13 at 13:14
  • @joeyrohan sorry, didn't understand. Can you elaborate? – Mikita Belahlazau Jan 16 '13 at 13:15
  • @NikitaBeloglazov what i mean to say is,having a insert statement(just insert 1 row into database) blocked my EDT, where as,this select statement gives me many rows as output,but don't block EDT.Why so? – joey rohan Jan 16 '13 at 13:19
  • @joeyrohan it's hard to tell. But usually inserting data to database much slower then selecting values from it. So it's possible that select runs pretty fast. – Mikita Belahlazau Jan 16 '13 at 13:29
  • @Nikita Beloglazov my answer here is for you, only EDT & Swing important rulles, not cabout oding or managements rulles – mKorbel Jan 16 '13 at 13:32
  • @joeyrohan: Nikita is right about the best case, but mKorbel is right about the worst case; off course, you have to code for the latter. :-) – trashgod Jan 16 '13 at 14:39

1 Answers1

2

Assuming the Swing GUI objects are constructed on the event dispatch thread (EDT), the query very definitely blocks the EDT for some indefinite period of time. As noted in Memory Consistency Properties:

Each action in a thread happens-before every action in that thread that comes later in the program's order.

The problem is not how short the time is under ideal conditions, it's how long the time may become when things go awry. GUI users are very sensitive to EDT liveness; a worker thread is good insurance against user dissatisfaction.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045