Discussion of threads about UI programs in eclipse

Source: Internet
Author: User
Tags thread

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.