Although the threading mechanism between the operating systems is different, it is roughly the same. When a user uses a GUI program, the operating system produces a GUI event that determines which window or program accepts each event and puts it in the program's event queue, when the mouse is clicked or the key on the keyboard is pressed.
The underlying structure of any GUI program is an event loop. The program first initializes the event loop, and start the loop, which receives the GUI events sequentially from the event queue and responds accordingly. The program should respond to the incident quickly so that the program always responds to the user, for example, the user points to a button in the program and the program doesn't Should be, then this program should be regarded as a failure of the program it.
If a UI event raises a transaction that takes a long time, then you should put it in a separate thread so that the program's loop of events will be back in response to the user's next action. Threads are a very complex subject, and if handled poorly it can easily lead to deadlocks and other bad situations.
Fortunately, Eclipse has provided us with a convenient UI thread pack to develop plug-ins that greatly simplifies many of the underlying complexities. Take a look at a few simple concepts first.
1.SWT UI Thread
SWT uses a thread pattern that is directly supported by the operating system, and the program runs a time loop in the main program and Chengri the response in this line. Look at this code, the UI thread is the thread that created the display.
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.open ();
// 开始事件循环
// 关掉窗口后
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ())
display.sleep ();
}
display.dispose ();
}
In a simple applet, a UI thread can be satisfied. But if it's a long time operation, you'd better not do it with the UI thread, you can give it to the job. It's actually another thread that starts up, which is the non-UI thread that I'm going to say.