Documentation

The Java™ Tutorials
Hide TOC
Initial Threads初始线程
Trail: Creating a GUI With Swing
Lesson: Concurrency in Swing

Initial Threads初始线程

Every program has a set of threads where the application logic begins. 每个程序都有一组应用程序逻辑开始的线程。In standard programs, there's only one such thread: the thread that invokes the main method of the program class. 在标准程序中,只有一个这样的线程:调用程序类的main方法的线程。In applets the initial threads are the ones that construct the applet object and invoke its init and start methods; these actions may occur on a single thread, or on two or three different threads, depending on the Java platform implementation. 在小程序中,初始线程是构造小程序对象并调用其initstart方法的线程;这些操作可能发生在单个线程上,也可能发生在两个或三个不同的线程上,具体取决于Java平台实现。In this lesson, we call these threads the initial threads.在本课中,我们将这些线程称为初始线程

In Swing programs, the initial threads don't have a lot to do. 在Swing程序中,初始线程没有太多工作要做。Their most essential job is to create a Runnable object that initializes the GUI and schedule that object for execution on the event dispatch thread. 他们最重要的工作是创建一个Runnable对象,该对象初始化GUI并计划该对象在事件分派线程上执行。Once the GUI is created, the program is primarily driven by GUI events, each of which causes the execution of a short task on the event dispatch thread. 创建GUI后,程序主要由GUI事件驱动,每个事件都会导致在事件分派线程上执行一个短任务。Application code can schedule additional tasks on the event dispatch thread (if they complete quickly, so as not to interfere with event processing) or a worker thread (for long-running tasks).应用程序代码可以在事件分派线程(如果它们很快完成,以免干扰事件处理)或工作线程(对于长时间运行的任务)上调度其他任务。

An initial thread schedules the GUI creation task by invoking javax.swing.SwingUtilities.invokeLater or javax.swing.SwingUtilities.invokeAndWait . 初始线程通过调用javax.swing.SwingUtilities.invokeLaterjavax.swing.SwingUtilities.invokeAndWait来调度GUI创建任务。Both of these methods take a single argument: the Runnable that defines the new task. 这两个方法都有一个参数:定义新任务的RunnableTheir only difference is indicated by their names: invokeLater simply schedules the task and returns; invokeAndWait waits for the task to finish before returning.它们的唯一区别在于它们的名称:invokeLater只是调度任务并返回;invokeAndWait等待任务完成,然后返回。

You can see examples of this throughout the Swing tutorial:您可以在Swing教程中看到这样的例子:

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        createAndShowGUI();
    }
});

In an applet, the GUI-creation task must be launched from the init method using invokeAndWait; otherwise, init may return before the GUI is created, which may cause problems for a web browser launching an applet. 在小程序中,必须使用invokeAndWaitinit方法启动GUI创建任务;否则,init可能会在创建GUI之前返回,这可能会导致web浏览器启动applet时出现问题。In any other kind of program, scheduling the GUI-creation task is usually the last thing the initial thread does, so it doesn't matter whether it uses invokeLater or invokeAndWait.在任何其他类型的程序中,调度GUI创建任务通常是初始线程做的最后一件事,因此它使用invokeLater还是invokeAndWait都无关紧要。

Why does not the initial thread simply create the GUI itself? 为什么初始线程不简单地创建GUI本身?Because almost all code that creates or interacts with Swing components must run on the event dispatch thread. 因为几乎所有创建Swing组件或与之交互的代码都必须在事件分派线程上运行。This restriction is discussed further in the next section.下一节将进一步讨论此限制。


Previous page: Concurrency in Swing
Next page: The Event Dispatch Thread