Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the thread of the SwingWorker () constructor?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about the thread of SwingWorker () constructor, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

The demo uses the SwingWorker () constructor to perform time-consuming operations in the background, and then updates the UI.

This new implementation is also based on the FutureResult class of DougLea's util.concurrent package. Because of the heavy reliance on the work done by FutureResult, the implementation of the SwingWorker class is simple and flexible.

The rest of this section describes the details of the implementation in more detail. Please read on or skip to the back to download the source code.

RunnableFutureResult

FutureResult, as its name implies, is used to maintain the result of an action. It is designed to be used with a Callable, which is a runnable action that returns a result:

PublicinterfaceCallable {Objectcall () throwsException;}

The new SwingWorker is a RunnableFutureResult. At run time, it sets the result to the return value of construct () and then calls the finished () method in the event dispatch thread. Note: SwingWorker is an abstract class; you want to subclass it and implement construct () and finished (). )

The following code comes from the run () method of SwingWorker:

Callablefunction=newCallable () {publicObjectcall () throwsException {returnconstruct ();}}; RunnabledoFinished=newRunnable () {publicvoidrun () {finished ();}}; setter (function). Run (); SwingUtilities.invokeLater (doFinished)

The * paragraph converts construct () into a Callable action, and the second paragraph converts finished () into doFinished as a Runnable. Then setter (function) is run and doFinished is called.

Setter (function)

The missing part above is setter (function). It creates a stereotyped Runnable. At run time, this Runnable calls the function specified by the parameter, and then sets the return value for the result. Here is the code from FutureResult:

PublicRunnablesetter (finalCallablefunction) {returnnewRunnable () {publicvoidrun () {try {set (function.call ());} catch (Throwableex) {setException (ex);};}

Pay attention to the protection made by the try-catch block. If construct () throws anything (Exception, Error, etc.), it will be captured and recorded.

Don't get a head start: first construct, then start

Call start () to start the worker thread. This is an important difference between the revised version of SwingWorker and the original version.

In the original version, the SwingWorker () constructor automatically started the thread, which posed the danger of a thread competing with the subclass constructor: when the SwingWorker () constructor had started the thread and the subclass constructor had not yet completed. The remedy is to construct the SwingWorker and then call start ().

By the way, RemoteTable does not call start (). Correctly, SwingWorker is executed by QueuedExecutor as a Runnable.

After reading the above, do you have any further understanding of the threads of the SwingWorker () constructor? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report