I. Why is the GUI A Single-threaded traditional GUI application usually is single-threaded.
1. You need to call the poll method to obtain input events at all points of the Code (this method will bring great confusion to the Code)
2. Indirectly execute all the code of the application through a "Main Event Loop.
If the code called in the main event loop takes a long time to be executed, the user interface will be frozen until the code execution is complete. This is because subsequent User Interface events can be processed only when the execution control is returned to the main event loop.
A lot of efforts to try a multi-threaded GUI framework are always caused by stability problems caused by static conditions and deadlocks, and return to the old path of the single-thread time queue model.
1. Process ordered events
Because only the unique thread is processing GUI tasks, all tasks do not need to be considered for concurrency and are executed in sequence. However, if the execution time in the task is too long, or subsequent operations cannot respond. (Android will prompt the error "Andorid Not Response)
2. Thread restrictions in Swing
Single-threaded GUI rules: Components and models can only be created, modified, and requested in the event dispatch thread.
In Andorid, If you create or update the UI in the sub-thread, an exception is thrown.
2. In the short-term GUI task GUI application, events originate from the event thread, and bubbles may have to be passed to the listener provided by the application, if it is relatively simple to modify the color, etc, it can be processed directly in the event thread.
Iii. Time-consuming GUI tasks because GUI tasks have thread restrictions, subthreads need to process time-consuming operations. At last, refresh the sub-threads.
1. Cancel
2. progress and completion Identification
3. SwingWorker
Use AsyncTask in Andorid
4. The simplest way to avoid responsiveness in the shared data model is to read data to the memory at one time during initialization. In this way, you need to consider whether to occupy too much memory.
1. Thread-Safe Data Model
ConcurrentHashMap cannot provide consistent data snapshots. CopyOnWriteArrayList simultaneously obtains thread security, consistency, and good responsiveness.
2. Data decomposition model
If a data model must be shared by multiple threads and cannot implement a thread-Safe Model Due to blocking, consistency, or complexity, you can consider using the decomposition model design.
5. Other forms of Single-threaded subsystems cannot avoid synchronization or deadlock in some cases, such as Native Library requirements and System. when loadLibrary is loaded, it must all be executed in the same thread.
Use Future and newSingleThreadExecutor together to process tasks that can be canceled by a single thread.