简体   繁体   中英

Why the following lines don't block the EDT?

    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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM