definition :

      thread : An execution process in a process

   
  The difference between thread and process : Each process needs an independent memory address space allocated by the operating system , All threads in the same process work in the same address space , These threads can share the same block of memory and system resources .

      deadlock : When a thread waits for a lock held by another thread , While the latter is waiting for a lock already held by the first thread , A deadlock will occur .

Implementation mode :

     Java There are two ways to implement multithreading , One is inheritance Thread class , Second, implementation Runnable Interface .

Thread state and its transition :

*
  When a thread executes start After method , Does not mean that this thread will be executed immediately , It only means that this thread is in a running state , Finally by OS To determine which runnable thread is executed .
*
There is a time limit for a thread to be selected for execution at one time , This time period is called CPU Time slice of , When the timeslice runs out but the thread is not finished , This thread will become runnable again , wait for OS Rescheduling of ; Execute in running thread Thread.yeild() Method can also make the current thread runnable .
* Waiting for user input in a running thread , call Thread.sleep(), Calling the join() method , The current thread becomes blocked .
* Blocked thread user input completed ,sleep Time out ,join End of thread for , The current thread changes from blocked to runnable .
* Running thread call wait method , This thread enters the waiting queue .
*
Running thread encountered synchronized At the same time, I didn't get the lock mark of the object , Thread waiting for queue wait Time out , The thread waiting for the queue is notify Method wake up , There are other thread calls notifyAll method , Then the thread becomes the lock pool state .
* Thread in lock pool state gets object lock token , The thread becomes runnable .
* Running threads run Method completed or main End of thread , End of thread running .
Method introduction :

* Thread.yield() The currently running thread becomes runnable .
* t2.join() Causes the current thread to block until t2 Thread execution completed .
* Thread.sleep() Causes the current thread to block until sleep End of .
*
wait,notify,notifyAll The way is Object Method of class , Its calling environment must have synchronized Call in synchronization block , Otherwise, it will be thrown java.lang.IllegalMonitorStateException abnormal .
sleep() and yeild() The difference between :

   
 1,sleep() Method gives other threads the chance to run , Regardless of the priority of other threads , So it gives lower threads a chance to run ;yield() Method only gives threads with the same or higher priority a chance to run .

     2, When the thread executes sleep(long
millis) After method , Will go to blocked state , parameter millis Specify sleep time ; When the thread executes yield() After method , Will go to ready .

     3,sleep() Method declaration throw InterruptedException abnormal , and yield() Method does not declare any exception to be thrown

     4,sleep() Method ratio yield() The method has better portability

About object locks :

When a thread attempts to access the synchronized(this) When a code block is marked , Must obtain this Lock of the object referenced by the key , There are two situations as follows :

     1,
If the lock is occupied by another thread ,JVM This thread will be put into the lock pool of this object . This thread is blocked . There may be many threads in the lock pool , Wait until the other threads release the lock ,JVM A thread will be randomly removed from the lock pool , Lock this thread , And go to ready .

     2, If the lock is not occupied by other threads , This thread will get this lock , Start execution of synchronization code block .
( In general, the synchronization lock will not be released when the synchronization code block is executed , But there are also special cases where an object lock can be released
Such as when executing synchronization code block , Thread termination due to exception , The lock will be released ; When executing a code block , Of the object to which the lock belongs wait() method , This thread will release the object lock , Enter the waiting pool of the object )

Characteristics of thread synchronization :

   
 1, If a synchronized code block and an unsynchronized code block operate on shared resources at the same time , There will still be competition for shared resources . Because when a thread executes an object's synchronization code block , Other threads can still execute unsynchronized blocks of code for objects .( So called synchronization between threads , When different threads execute the synchronized code block of the same object , To get the synchronization lock of an object and hold each other )

     2, Each object has a unique synchronization lock  

     3, You can use before static methods synchronized Modifier .

     4,
When a thread starts executing a synchronized block of code , Doesn't mean you have to run it all the time , The thread entering the synchronization code block can execute Thread.sleep() Or execution Thread.yield() method , It does not release the object lock at this time , Just give the chance to run to other threads .

     5,
Synchronized Declaration will not be inherited , If one uses synchronized Decorated method is covered by subclass , This method is not synchronized in the subclass , Unless synchronized modification .

Thread safe classes :

     1, Objects of this class can be accessed safely by multiple threads at the same time .

     2, Each thread can perform atomic operations normally , Get the right results .

     3, After the atomic operation of each thread is completed , The object is in a logical and reasonable state .

Release lock on object :

    1, When the synchronization code block is executed, the lock of the object will be released

    2, In the process of executing synchronization code block , Thread termination due to exception , The lock will also be released

    3, In the process of executing synchronization code block , Of the object to which the lock belongs wait() method , This thread will release the object lock , Enter object's waiting pool .

Technology
©2019-2020 Toolsou All rights reserved,
Mybatis Error resolution :There is no getter for property named '*' in 'class Java.lang.String Big data tells you , How tired are Chinese women Message quality platform series | Full link troubleshooting Gude Haowen serial - You deserve to be an engineer ( Preface ) Image explanation of over fitting and under fitting Springboot of JPA Common query methods JAVA Detailed explanation of anomalies vue Of v-if And v-show The difference between python To solve the problem of dictionary writing list in Codeup——601 | problem A: task scheduling