Java thread learning foundation

xiaoxiao2021-03-06  43

1. Some basic terms and concepts 1.1 Threads of the thread Several Status Initialization Status When the Status Status Block Status Termination 1.2 Daemon Thread DAEMON Thread Differences General Threads Yes: Once the main program ends, the daemon thread will end. 1.3 Lock Definition In order to coordinate multiple concurrent running threads to introduce the concept of locks. 1.4 Deadlocking Any multi-threaded application has deadlock risk. When each of a set of threads is waiting for an event that only one thread in the group can be caused, we say that this set of threads will be locked. To change a saying, it is possible to say that this set of threads entered the dead lock in a set of resources waiting for the resource of other members. The simplest situation of the deadlock is: thread a holds the exclusive lock of the object X, and is launched by the lock waiting object Y, and the thread b holds the exclusive lock of the object Y, but the lock waiting for the object X. Unless there is a way to break the lock, the deadlock will be waited forever. 1.5.5.java objects Several methods about the lock 1.5.1 WAIT method WAIT method is the method of the Java root object Object, indicating waiting for a lock. Before the WAIT method enters, the corresponding lock will be released, and a lock will be obtained again when the wait method returns. If the wait () method does not have parameters, it is possible to get the lock of the object again when the other thread holds the object lock. If the wait () method is parameters, such as: Wait (10), when holding the object lock, the NOTIFY or NotifyAll method is called, or the specified time has passed, and it is possible to get the lock again. Refer to thread.lock.sleepandwait1.5.2 Notify / NotifyAll method I will no longer explain. Haha, steal some lazy. 1.5.3 Yield method Yield () will automatically discard the CPU, sometimes more improved than SLEEP. 1.6 Lock Object (Lock of Example Method) When using a lock in the synchronous code block, the object that is the lock can be the object itself or a separate object, but must ensure that the lock object cannot be empty. If you lock a NULL object, an exception is generated. In principle, do not select an instance variable that may change the value in the lock scope as a lock object. Lock objects, one is the object yourself, one is to define a normal object as Private Property, and another is to create a new class and then use the instance of this class to be used. Reference: Thread.lock.useselfaslock, use the object yourself to do lock object thread.lock.useObjaslock Use an instance object to lock object thread.lock.useafinalObjaslock Use a constant object as a lock object 1.7 lock instance method existence of synchronization issues, Similarly, The type method also has a case where you need synchronization. The class lock of the general class method is a Static Object to be held. Of course, you can also use the class object of the class itself as a class lock. An instance method of a class can get the instance lock, but also try to access the class method, contain the class synchronization method, and get the class lock. A class of classes, you can try to get a class lock, but you can't try to get the instance lock directly. Mr is needed to be an example and then the instance lock of this instance is obtained. Refer to Thread.Lock.useStaticObJasstaticLock Use the class's properties object as a class lock. Thread.lock.useclassasstaticLock uses class class objects as class lock 1.8. Thread security method and thread unsafe method If all of an object's PUBLIC method is synchronous method, that is, the public method is thread security, then the object The Private method, which can be set to not a thread security without considering inheritance.

Refer to thread.lock.synmethrodandNotSynmethrod1.9 class locks and instance locks Use the class locks and instance locks in the instance method; can be used in accordance with the example locks and class locks as previously mentioned. The class lock and instance lock can be used in the class method, and the class lock can be used as before, in order to use the instance lock, first generate an instance, and then instance locks. Refer to the particle size problem of thread.lock.staticlockandobjlock1.10 lock. In order to solve the particle size of the object lock, the possibility of causing the deadlock is increased, and the particle size of the lock is fine, and the work will be increased. For the size of the lock, this is completely considered according to the actual development needs, it is difficult to have a unified standard. 1.11. Read and write a read-write lock supports multiple threads to access an object at the same time, but only one thread can modify this object at the same time, and cannot be modified when access is performed. There are two types of dispatch strategies, one is to read the lock, and the other is the write-off. Refer to thread.lock.readwritelock1.12 Volatile Sets the operation of the variable value in Java, in addition to the LONG and DOUBLE types of variables are atomic operations, that is, the simple read and write operations for variable values ​​are not necessary to synchronize. Before JVM 1.2, Java's memory model implementation always reads variables from the main memory, which is not required for special attention. With the maturity and optimization of JVM, the use of Volatile keywords is now very important in multithreaded environment. Under the current Java memory model, threads can save variables in local memory (such as machine registers), rather than reading and writing directly in the main memory. This may cause a thread to modify a variable value in the main memory, while another thread also continues to use the copy of the variable value in the register, resulting in inconsistent data. To solve this problem, you only need to declare this variable as Volatile, which indicates JVM, this variable is unstable, and this variable is unstable. Read. Generally speaking, the flags of each task between the multi-task environments should be added to Volatile modification. 2. Communication between threads In other languages, threads can achieve communication between threads, shared memory, pipes, etc., but in this manner, it is concerned that the synchronization between threads is concerned. Just ensure that the thread is safe to run, the information sharing is natural to appear. 2.1 Barrover is such a wait point: a set of threads are synchronized at this point, these threads merge their respective results or run to the next phase of the overall task. Reference: thread.lock. Barrieruseexamplethread.lock.barrier2.2. Lock tool class provides access, release functionality to thread locks. The acquisition process of the lock is displayed. Can be used as a tool class. Reference: thread.lock. Busyflag2.3. Condition variable condition variables are a synchronization type provided by the POSIX thread model, and similar to the Waiting notification mechanism in Java. Although Java has already waited for notification mechanism, in order to reduce the overhead of thread schedules in the Notify / NotifyAll method, some threads that do not need to be activated are shielded, and the conditional variables are introduced. Two (multiple) condition variables in Java can be the same mutex (lock object).

Reference: Thread.lock.condvar Conditional Variable Category: A lock controls multiple signal channels (eg: multiple variables), although simple Java waits for notification mechanisms, but thread scheduling efficiency is not high, and thread readable Nothing is not too good, at this time, you can use a lock object (busyflag instance), using this busyflag instance to create multiple conditional variables (CONDVAR instance). Places that are often used to Condvar are buffer management, such as pipe operations. Create a busyflag instance first, then create a CONDVAR instance, describe whether the buffer is empty with this condition variable, and create a CONDVAR instance for condition variable buffer. In reality, the traffic light of the road can be described by using condition variables. 3. Java Thread Scheduling 3.1 Java Priority Java's priority has 10 priorities, plus the priority levels you use by virtual machines = 0, a total of 11 kinds. In most cases, the higher the priority setting of the Java thread (highest = 10), the thread is more prioritized. 3.2. Green thread thread runs within the virtual machine, the operating system does not know the existence of such threads at all. The thread is scheduled by the virtual machine. 3.3 Local thread thread is done by the operating system running the virtual machine. 3.4 Windows Local Thread Operating System, it is fully able to see each thread in the virtual machine, while the thread of the virtual machine thread and operating system is one or one. The Java thread scheduling room is determined by the operating system underlayer. Under the Win32 platform, the Windows thread has only 6 priority. And java thread priority level corresponds to the following: Java thread priority Windows 95 / nt / 2000 thread priority 0 THREAD_ PRIORITY_IDLE1 (Thread.MIN_PRIORITY) THREAD_ PRIORITY_LOWEST2 THREAD_ PRIORITY_LOWEST3 THREAD_ PRIORITY_BELOW_NORMAL4 THREAD_ PRIORITY_BELOW_NORMAL5 (Thread.NORM_PRIORITY) THREAD_ PRIORITY _NORMAL6 THREAD_ PRIORITY _ABOVE_NORMAL7 THREAD_ PRIORITY _ABOVE_NORMA8 THREAD_ pRIORITY _HIGHEST9 THREAD_ pRIORITY _HIGHEST10 (Thread.MAX_PRIORITY) THREAD_ pRIORITY _CRITICAL3.5 thread priority inversion and inheritance If a thread holds the lock (assuming that the thread name = ThreadA, priority level = 5), another thread (assuming that the thread Name = threadb, priority = 7), now this threada is running, but thread threadb requests to hold THREADA's lock, this time, thread A increases its operational priority ( Improve the priority level = 7), and thread threadb reduces thread priority level (low to Threada original priority level = 5). The above situation, inheriting the priority of ThreadB, This has become a priority level; for ThreadB temporarily reduces priority, it is an inversion of priority. Of course, once the locks held by thread Threada are released, their priority will return to the original priority (priority = 5). Thread threadb obtains the corresponding lock, the priority level will also return to the original value (priority = 7).

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

New Post(0)