Java thread

xiaoxiao2021-03-06  28

1. Threads are used in several aspects:

Increasing the response speed of the GUI application utilizes multiprocessor systems When there are multiple independent entities, simplifying the program logic to perform blocking I / O 2 without blocking the entire program. If the object has a thread, it should provide a start () or init () method that starts the thread instead of starting it from the constructor.

3. Thread end mode:

The thread reaches the end of its RUN () method. The thread throws an unfained Exception or Error. Another thread calls an abandoned STOP () method. Abandoning means that these methods still exist, but you should not use them in the new code and should try to remove them from existing code. 4. Wait other thread: The Thread API contains methods waiting for another thread to complete: Join () method. When THRead.join () is called, the calling thread will block until the target thread is completed. 5. The thread sleep Thread API contains a SLEEP () method, which will enable the current thread into the waiting state until a specified time, or until another thread calls thread.interrupt (), thus interrupted Thread. When the specified time is passed, the thread will turn it to run, and returns to the running line queue of the scheduler. Daemon thread system thread is called a daemon thread. The Java program is actually exiting after all of its non-daemon threads are completed. Any thread can become a daemon thread. You can specify a thread by calling the Thread.SetDaemon () method to be a daemon thread. These threads are only useful when other non-daemon threads are running. 7. Synchronization of controlled access To ensure that data can be shared between threads, Java languages ​​provide two keywords: Synchronized and Volatile. Synchronized has two important mean: it ensures that only one thread can execute the protected section (mutual exclusive, MUTUAL EXCLusion, or Mutex), and it makes it possible to change the data for other threads (Changed) Visibility). 8. The Volatile is simpler than synchronization, which is only suitable for access to a single instance of basic variables (integer, Boolerge, etc.). When a variable is declared into Volatile, any write operation to the variable will bypass the cache, directly written to the main memory, and any reads of the variable also bypass the cache, directly take independent memory. This means that all the Volatile variable values ​​seen at all times are the same. 9. Dead locks To avoid dead locks, be sure to get the lock in all threads in all threads when getting multiple locked. 10. When the SYNCHRONIZED block, there are several simple guidelines to follow: 1) keep the code block short. Synchronized blocks should be short - as short as possible while ensuring the integrity of related data operations. The Synchronized block is removed from the pretreatment and post-processes that do not change the thread. 2) Do not block. Do not call methods that may cause blocking in the Synchronized block or method, such as INPUTSTREAM.READ (). 3) Do not call methods for other objects when holding locks. This can sound some extreme, but it eliminates the most common deadlock source.

Although the thread API is very simple, the program to write thread security is not easy. When sharing variables between threads, you must be very careful to ensure proper synchronization of reading and writing access. When writing a variable that may be read by another thread, or reading a variable that may be written by another thread, synchronization must be used to ensure that the changes to the data are visible between the thread.

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

New Post(0)