Tame Java thread (1)

zhaozj2021-02-17  62

Java thread architecture

Java multi-threaded programming is actually as simple as most of the books, all Java programming of the UI (user interface) involves multi-threaded. This chapter will affect the Java multi-thread program by discussing the thread architecture of several operating systems and these architectures. According to this idea, I will introduce some inadvertent key terms and concepts described in Java entry-level books. Understanding these concepts is to enable you to understand the essential conditions for the examples provided by this book.

Multithreaded programming

The like ostrich puts his head in the sand, pretending that it is not considered multi-threaded problems. It is actually a lot of people in Java programming. But in a real product, you can't avoid this serious problem. Currently, most of the books on the market are very beautiful, even the examples they provide, they cannot operate correctly in multithreaded environments.

In fact, multithreading is an important factor affecting all code. It can be extremely said that single-threaded code is worthwhile in real application, even simply can't run, not to say correctness and efficiency. So you should use multiple threads as an important aspect from the beginning, integrate your code architecture.

All extraordinary Java programs are multi-threaded

Whether you like or not, all Java programs are multithreaded in addition to a small part of the very simple console program. The reason is that Java's Abstract Windowing Toolkit (AWT) and its extended swing, AWT processes all operating system-level events with a special thread, which is generated when the first window occurs. Therefore, almost all AWT programs have at least 2 threads in operation: one is the thread where the main function is located and processes the response method of the listener from OS and the listener of the registration (that is, the callback function) AWT thread. It must be noted that all registered listener methods are running on the AWT thread, rather than people generally believed the main function (this is also a thread registered by the listener).

This architecture has two problems. First, although the method of listener is on the AWT thread, they are actually inner-classes declared on the MAIN thread. Second, although the method of listener is on the AWT thread, it generally accesses its external class very frequently, which is the member variable of the class running on the MAIN thread. When these two thread competition (Compete) access to the same object instance (Object), a very serious thread synchronization problem is caused. Appropriate use Keyword Synchronized is the necessary means of ensuring two threads secure access to shared objects.

Worse, the AWT thread not only stops the listener method, but also responds to an event from the operating system. This means that if your listener method takes up a lot of CPU time to process, your program will not be able to respond to operating system-level events (such as mouse click event and keyboard events). These events will be blocked in the event queue until the listener method returns. The specific performance is the deadlock of the UI. This will make users unacceptable. Listing 1.1 is an example of such an unpredictable UI. The program generates a frame containing two buttons. The SLEP button is allowed to sleep (which is the AWT event processing thread) sleep for 5 seconds. The Hello button is just simple to print "Hello World" on the console. Within the Sleep button for 5 seconds, no matter how many Hello buttons you are pressed, there will be no response. If you press the Hello button 5 times during this period. Then "Hello World" will be immediately printed five times, and after the listener method of your SLEEP button is over. This proves that 5 mouse click events are blocked in the event queue until the Sleep button is running.

Import javax.swing. *; import java.awt. *; import java.awt.event. *; class hang () {jButton b1 = new jbutton ("Sleep"); jbutton b2 = new jbutton "Hello");

B1.AddActionListener (new actionListener () {public void action {try {thread.currentthread (). Sleep (5000);} catch (exception e) {}}});

B2.AddActionListener (New ActionListener () {Public Void ActionPerformed (ActionEvent Event) {System.out.Println ("Hello World");}});

GetContentPane (). setLayout (); getContentPane (). Add (b1); getContentPane (); add (b2); pack (); show ();}

Public static void main (string [] args) {new hang ();}}

The Java GUI discussed in most books has avoided the problem of threads. In reality, it is not advisable to taking a single thread for the UI event. All successful UI programs have the following common:

l ui must give feedback information to the user on the running state process of the program. The simple pop-up dialog box is not enough. You must tell the user to operate the operational progress (eg, a percentage schedule).

l Must be done when the underlying system status changes, the entire UI will not be regenerated for an update window.

l You must make your program, when the user clicks on the Cancel button, your program can respond immediately and terminate in time.

l Must do it when a time you need to run is running, users can do other operations on your UI interface.

This is the rule of the rules to summarize: do not allow the deadlock UI interface, not allowing the program to run one

When a long time is taken, other operations are ignored, such as mouse click and keyboard events, so it is not allowed to run long-time operations in the listener window. Time-consuming operations must be run in other threads in the background. Therefore, the real program will run more than 2 threads at any time.

转载请注明原文地址:https://www.9cbs.com/read-29314.html

New Post(0)