This note records some common keywords in Java multi-threaded programming. It is relatively simple and basic, so it will not be too detailed. I. Thread class (actually the Runnable interface is applied) and Runnable interface (there is only one run method, and the run method must be overwritten in the application class). Generally, we define a Thread class, it can inherit the Thread class or the Runnable interface of the application. The run method must be rewritten no matter which method it inherits. For example, we define a Thread class: public class Threadtest implements Runnable {@ Override void run () {// write the operations you need for this process} then generate the thread object in the main method and call the start () method to run: public static void main (String args []) {new Thread (new Threadtest ()). start () ;}generally, a Thread has the following States: New (new): in this state, we generate a New Thread object through the new Keyword and allocate memory space for it, however, this thread is not in the alive status; ready (Runnable): The thread object calls the start () method, and the thread enters the runnable status, waiting to allocate CPU resources. At this time, the thread is alive. run (Running): After obtaining CPU resources, the thread starts to run and the run () method is executed until the thread ends, or it is interrupted or suspended due to resource depletion or other reasons; Blocked: the thread calls join (), sleep (), wait () and other methods and is temporarily Blocked; dead: The end of the thread, or the thread is forced to be interrupted or exited due to exceptions. The lifecycle ends. Iii. sleep method and wait method 3.1 and sleep method (only CPU is released, no lock is released). For example, a variable I is used by several threads, thread A calls the sleep method to only release the CPU, but thread A does not release the I lock, and the several threads just need I to meet certain conditions to continue running, so thread A is useless even if the CPU is released. 3.2. wait method (release CPU and lock). For example, there are two processes. After process A calls the wait method, process A releases the CPU and lock, wait for thread B to call the notify method or the notifyAll method to notify A. you can snatch resources, but A may not be able to grab the resources. 4. synchronized: for example, a resource j is used by several threads. We call it a competitive resource (race condition ), for such shared resources, we need to perform synchronization operations to avoid one thread's operations on this variable being overwritten by another thread. There are usually two ways to use: 1. The public/private/protected function returns the synchronized function name (form parameter) {// function content} example: public void synchronized FuncName () {// do something} 2. public/private/protected function return type function name (shape parameter) {synchronized (this) {// the content of the function to be synchronized} // other operations} below is a typical interview question: Give the variable j to you and ask you to write two functions (threads) one is to accumulate and the other is to decrease it. Then four threads are generated in the main function, two threads are accumulated, and two threads are decreasing. View Code 5. The Lock method and the unLock method jdk5.0 versions and earlier versions only have synchronized. Later on, the Lock method was available, and synchronized can do everything it could do, in addition, the acquisition and release of locks are more explicit, because programmers need to clearly give the time to release the locks, which is a more rigorous mechanism. This usage is also relatively simple. Let's first give the following class definition for easy explanation: the Code above View Code has not been compiled and run, because it is directly typed in the text document, that's what it means. When you want to write a competing resource, you first obtain the lock so that other processes cannot access it. After the operation is complete, the lock is released. In order to have an example that can be determined to run, we copied the testing Code that can be run by others. For View Code, if it is read by multiple threads and written by one thread, you only need to lock the process for write operations. Read operations generally do not need to lock those threads.