7

Action in concurrent thread not executed when main thread is pending

 2 years ago
source link: https://www.codesd.com/item/action-in-concurrent-thread-not-executed-when-main-thread-is-pending.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Action in concurrent thread not executed when main thread is pending

advertisements

I have an application which use temporary tables (existed in the scope of user session) and complex data processing.

For debug purposes I need to execute queries to this temporary tables during the application processing.

I added some additional logic as aspects (AspectJ) and run my application as load-time weaving application in Eclipse (using AJDT/ JDT weaving plugin).

I do the following: after getting new connection I create another thread with gui and pass the connection to it (would be described later).

After each query the main application thread waiting for the message from gui thread to continue the work (that gives me an opportunity to make queries and see intermediate results in temporary tables). Messaging is implemented using BlockingQueue.

In the GUI I have a frame with text area for query and two buttons "Run query" and "Release main thread".

I wanted that pressing the "Run query" button will execute the query and show the results on the frame. And pressing the button "Release main thread" will send the message to the main thread to continue the work.

The problem is when the main thread is waiting for blockingQueue.take(), pressing the button "Run query" causes the frame to freeze and do nothing (it looks like gui becomes unresponsive).

When the main thread is waiting for blockingQueue.take(), "Release main thread" forks fine (but not after "Run query" pressing).

When the main thread is running (I put plenty of objects in the queue), "Run query" button works normally and I can see query results.

At first I've tried manipulations with EDT and events dispatching, but nothing has helped me. Have you any ideas of the problem?

//aspect on 'newConnection' pointcut
after() returning (final Connection connection): newConnection()  {
    gUI = new Runnable() {
        public void run() {
            new GuiView(blockingQueue, connection);
            }
        };
    SwingUtilities.invokeLater(gUI);
}

    //GuiView code extract for button with query data retrieval action

    button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
    try {
      ResultSet rs = ConnectionManipulator.executeStatement(
          queryTextAreaG.getText()
      );
    ///....result parsing logic

    } catch (SQLException e1) {
        JOptionPane.showMessageDialog(null, "Exception!");
    } finally {

    }
    //....result out logic

    }
});


GUIs usually run in the main thread (not only on Java), so if you let your main thread block, you block your GUI as well and it freezes.

EDIT

I think you got SwingUtilities.invokeLater wrong: it is being used to execute part of the code in the main event loop thread, so usually is being called from the background thread, not the other way round. Now it looks like the query were being executed in the event loop thread thus blocking GUI.

Let the query be executed in a normal thread and when it is ready call the mainthread callback using SwingUtilities.invokeLater.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK