In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to analyze SwingWorker and Swing programs, the content is very detailed, interested friends can refer to, hope to be helpful to you.
The fact that someone slapped my head gave me a chance to do it with Swing, because no one on the project team had ever done this thing with this thing, so I had to stick to it! Sometimes people have to be a little stressed.
The first phase of development:
Preparation phase: the selection of tools. Using Netbeans 6, try, feel the interface is relatively easy, but the generated code is very long, at first very strange to Swing, watching netbeans generate code is a headache, feeling is not what I want, and then give up. Then there was VE, because it was just a tumble and didn't take a closer look. I chose Jigloo, but I didn't know that the code he generated was bad until I used it.
Development phase: do not be familiar with swing development, in which you encounter a lot of chaos, but because the function of this software is relatively simple, * finally came out. Although there is a pile of BUG, it can be regarded as my * swing work. Customers feel that the effect is not ideal (there are software design aspects and technical application aspects), so they have the experience of continuing development below.
The main technical problem is that the misuse of threads leads to deadlocks, often resulting in inexplicable fake death.
The second phase of development:
Because the first phase of development was more painful, I decided to change to a different language. Because RIA is relatively hot recently, the air of adoble in RIA is also quite popular, UI makes people feel very gorgeous, and I have tried to do some DEMO myself. Webservice used in the project, encryption and decryption operations, file uploads and downloads all have solutions, but the fatal thing is that external procedures should be called in this project, air is relatively fragile in this respect, google an as fscommand can call other procedures, but air unexpectedly does not support this, and later it is still solid to use SWING.
Then I tried a VE, and it felt much better than the last time I used it, and then I changed the Jigloo for VE.
What lies ahead is how to make good use of threads. Another google, found the swingworker this thing pull. SwingWorker introduced from Java SE 6 can help you easily write multithreaded Swing programs, improve the structure of your Swing programs, and improve the flexibility of the interface response, which is exactly what I want.
There are generally three types of threads in a Swing program:
◆ initialization thread (Initial Thread)
◆ UI event scheduling thread (EDT)
◆ task thread (Worker Thread)
Only one Swing program uses EDT, and this thread is responsible for drawing and updating GUI components, responding to user interaction by calling the program's event handler. All event handling is carried out on EDT, and the interaction between the program and UI components and its basic data model is only allowed on EDT. All tasks running on EDT should be completed as soon as possible, so that UI can respond to user input in time.
You should pay attention to the following when programming with Swing:
1. Accessing UI components and their event handlers from other threads can result in interface updates and drawing errors.
two。 Performing time-consuming tasks on EDT causes the program to become unresponsive, which prevents GUI event blocking from being handled in the queue.
3. Separate task threads should be used to perform time-consuming computing or input-output-intensive tasks, such as database communication, accessing website resources, and reading and writing large tree data files.
What I developed in the first phase was precisely because I did not pay attention to this, which led to the poor effect of the whole program. There is an event handler in the program that accesses Web services, which usually take many seconds to respond, during which time, if the program interacts with Web services on EDT, the user cannot cancel the search or interact with the interface, neither of which should run on EDT.
The javax.swing.SwingWorker class is a new class in Java SE 6. With SwingWorker, the program can start a task thread to query asynchronously and return the EDT thread immediately. Shows that event handling returns immediately after using SwingWorker, allowing EDT to continue with subsequent UI events. Originally, it was all put on EDT, and the effect was barely inevitable. Using Swingworker to start a task thread can flexibly respond to the interface.
Let's talk about his usage:
SwingWorker is defined as follows: public abstract class SwingWorkerextends Object implements RunnableFuture
SwingWorker is an abstract class, so you must inherit it to perform the specific tasks you need. Note that this class has two type parameters: t and V. T is the return type of the doInBackground and get methods, and V is the data type to be processed by the publish and process methods.
SwingWorker implements the following interface methods:
◆ boolean cancel (boolean mayInterruptIfRunning)
◆ T get ()
◆ T get (long timeout, TimeUnit unit)
◆ boolean isCancelled ()
◆ boolean isDone ()
SwingWorker implements all the interface methods. In fact, you only need to implement the following abstract methods of SwingWorker: protected T doInBackground () throws Exception
The doInBackground method executes as part of the task thread, which is responsible for completing the basic task of the thread and takes the return value as the result of the thread's execution. The inheriting class must override the method and ensure that it contains or proxies the basic task of the task thread. Instead of calling this method directly, use the execute method of the task object to schedule execution.
After obtaining the execution result, you should use the get method of SwingWorker to obtain the result of the doInBackground method. The get method can be called on EDT, but it will remain blocked until the task thread completes. The get method is called only when the result is known, so that the user does not have to wait. To prevent blocking, you can use the isDone method to verify that the doInBackground is complete. In addition, calling the method get (long timeout, TimeUnit unit) will block until the task thread ends or times out. The * place to get the task result is in the done method: protected void done ()
After the doInBackground method completes, SwingWorker calls the done method. If tasks need to update GUI components or do some cleanup with thread results after completion, you can override the done method to complete them. This is the * place to call the get method, because now that you know that the thread task is complete, SwingWorker activates the done method on EDT, so you can safely interact with any GUI component within this method.
There is no need to wait for the thread to complete to get intermediate results. The intermediate result is the data that the task thread can produce before it produces the result. When the task thread executes, it can publish intermediate results of type V, overriding the process method to handle the intermediate results. More details on these methods will be provided later. When a property changes, the SwingWorker instance notifies the processor that SwingWorker has two important properties: state and process. Task threads have several states, represented by the following SwingWorker.StateValue enumerated values:
◆ PENDING
◆ STARTED
◆ DONE
The task thread is in the PENDING state as soon as it is created. When the doInBackground method starts, the task thread enters the STARTED state. When the doInBackground method is completed, the task thread is in the DONE state. As the thread enters each stage, the SwingWorker superclass automatically sets these state values. You can add processors to receive notifications when these properties change.
Finally, the task object has a progress attribute that can be updated from 0 to 100 to identify the progress of the task as the task progresses, and when the attribute changes, the task notifies the processor to process it.
My sense of use is that time-consuming operations such as Icano operations, data operations, and network operations are processed in doInBackground (). Instead of publishing data at the end of the task, the publish method is called.
When the publish method, the SwingWorker class dispatches the process method. Interestingly, the process method is executed on top of EDT, which means that you can interact directly with Swing components and their models. You can give a progress bar when you are dealing with a task.
On how to analyze SwingWorker and Swing programs to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.