Running code on the main thread from a secondary thread?

前端 未结 5 547
灰色年华
灰色年华 2020-12-05 10:23

This is a general Java question and not an Android one first off!

I\'d like to know how to run code on the main thread, from the context of a secondary thread. For e

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 10:55

    You may want to use the 'even dispatching thread' where most event driven things happen. If you are using swing then:

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Your code here.
            }
        });
    

    Or create a class that implements Runnable and pass it into invokeLater().

提交回复
热议问题