In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What this article shares with you is about how to realize the instantiation of SwingWorker. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
SwingWorkersanexampleofusingSwingWorker: to use the SwingWorker class, you first need to implement a subclass of it. In the subclass, you must implement the construct method and include your long-time operations. When you instantiate a subclass of SwingWorker, SwingWorker creates a thread but does not start it. You will call the start method of your SwingWorker object to start the thread, and then the start method will call your construct method. When you need the object returned by the construct method, you can call the get method of the SwingWorker class. This is an example of using the SwingWorker class:
. / / in main method: finalSwingWorkerworker=newSwingWorker}; worker.start;. / / in action event handling method: JOptionPane.showMessageDialog)
When the program's main method calls the start method, SwingWorker starts a new thread to instantiate the ExpensiveDialogComponent. The main method also constructs a GUI consisting of a window and a button. When the user clicks the button, the program will block and, if necessary, block until the ExpensiveDialogComponent creation is complete. The program then displays a modal dialog box containing ExpensiveDialogComponent. You can find the whole program at MyApplication.java. Use the Timer class Timer class to perform an operation through an ActionListener or multiple times. When you create a timer, you can specify how often the operation will be performed, and you can specify a listener for the timer's action event. After the timer is started, the action listener's actionPerformed method is called to perform the operation. The actionPerformed method defined by the timer action listener will be called in the event dispatch thread. This means that you don't have to use the invokeLater method in it. This is an example of using the Timer class to implement an animation loop:
PublicclassAnimatorApplicationTimer extendsJFrameimplementsActionListener publicvoidstartAnimationelse} publicvoidstopAnimation publicvoidactionPerformed...}
Executing all user interface code in one thread has the advantage that component developers do not have to have a deep understanding of thread programming: all components in toolkits like ViewPoint and Trestle must fully support multithreaded access, making extension very difficult, especially for developers who are not proficient in thread programming. Some recent toolkits, such as SubArctic and IFC, use a similar design to Swing. Events are dispatched in a predictable order: invokeLater queued runnable objects are dispatched from the same queue of mouse and keyboard events, timer events, and drawing requests. In toolkits where components fully support multithreaded access, component changes are interspersed with event handling by fickle thread schedulers. This makes full testing difficult or even impossible. Lower cost: a toolkit that tries to carefully lock the critical area spends a lot of time and space on lock management. Whenever a method is called in the toolkit that may be implemented in the client code, the toolkit saves its state and releases all locks so that the client code can acquire the lock if necessary. When control is handed back to the toolkit, the toolkit must re-grasp its lock and restore its state. All applications have to bear this price, even if most applications do not require concurrent access to GUI. This is SubArcticJavaToolkit's description of the problem of supporting multithreaded access in the toolkit: our basic tenet is that extreme care must be taken when designing and building multithreaded applications, especially those that include GUI components. The use of threads can be deceptive. In many cases, they perform extremely well in simplifying programming, making it possible to design "simple autonomous entities that focus on a single task." In some cases, they do simplify design and coding. However, in almost all cases, they make debugging, testing, and maintenance much more difficult or even impossible.
No matter the practice that most programmers receive, their experience and practice, or the tools we use to help ourselves, can not be used to deal with indeterminism. For example, full testing is almost impossible when bug depends on time. Especially for Java, a program must run on the operating system platforms of many different types of machines, and each program must work properly under preemptive and non-preemptive scheduling. Because of these inherent difficulties, we urge you to think twice about whether it is absolutely necessary to use threads. However, there are situations where threading is necessary, so subArctic provides a thread-safe access mechanism. This chapter discusses this mechanism and how to safely manipulate the interaction tree in a separate thread. The thread safety mechanism they are talking about is very similar to the invokeLater and invokeAndWait methods provided by the SwingUtilities class.
The above is how to realize the instantiation of SwingWorker. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.