Multithreading
Process : The program in progress. In fact, a process is a memory allocation space when an application runs.
Threads : In fact, it is a program execution control unit in the process, an execution path. The process is responsible for the application's spatial labeling. Threads are responsible for the order in which the application executes.
Returns the name of the current thread:thread.currentthread (). GetName ()
The name of the thread is defined by the: thread-number. Numbering starts from 0.
The code for the thread to run is stored uniformly in the Run Method the.
The first way to create a thread is to inherit the thread, which is a subclass of the Run method.
Steps:
1 , the definition class inherits the thread class;
2 , the purpose is to copy the Run method, and the code that will let the thread run is stored in the Run method;
3 , creating a thread object by creating a subclass object of the thread class;
4 , invokes the thread's Start method, opens the thread, and executes the Run method.
The second way to create a thread: Implement an interface runnable.
Steps:
1 , the definition class implements the Runnable interface.
2 , overwriting the Run method in the interface (used to encapsulate the code that the thread is running).
3 , the thread object is created through the thread class;
4 , the subclass object that implements the Runnable interface is passed as the actual parameter to the constructor in the thread class.
Why should it be passed? Because you want the thread object to explicitly run the object that the Run method belongs to.
5 , call the Start method of the Thread object. Open the thread and run the running method in the Runnable interface subclass.
Thread State:
is created: Start ()
Run: Be qualified for execution and have the right to execute;
Freeze: Sleep (time), wait ()-notify () The thread releases the execution and releases the execution qualification;
Temporary blocking status: The thread has the CPU execution qualification, does not have the CPU execution right;
Extinction: Stop ()
Sync (synchronized):
Benefits: resolves a thread safety issue.
Malpractice : Relatively low performance because the lock needs to consume resources, resulting in deadlocks.
defining synchronization is a prerequisite :
1 , you must have two or more two threads to synchronize.
2 , multiple threads must ensure that the same lock is used.
The second manifestation of synchronization:
Synchronization Functions : In fact, the synchronization keyword is defined on the function, so that the function has synchronization.
Which lock does the synchronization function use?
By verifying that the function has its own object this, the lock used by the synchronization function is the this lock .
Sync Deadlock : Typically, you can see the phenomenon as long as you nest the synchronization. There is a synchronous code block in the synchronization function, and a synchronization function in the synchronization code block.
inter-thread communication : Thinking: Multiple threads are working on the same resource, but the action is different.
1 : Encapsulates a resource as an object.
2 : The task that the thread performs (the task is actually the Run method.) ) is also encapsulated as an object.
wait for wake-up mechanism: the method involved:
Wait: The thread in the synchronization is frozen. Release the executive right and release the eligibility. Store thread objects in the thread pool at the same time.
Notify : wakes one of the waiting threads in the thread pool.
Notifyall: all threads in the thread pool are awakened.
wait and sleep differences: analyze these two methods: from the executive right and the lock to analyze:
wait : You can specify a time or you can specify no time. No time is specified and can only be awakened by the corresponding notify or Notifyall.
Sleep : The time must be specified to automatically turn from the frozen state to the running state (temporary blocking state).
wait : Threads release execution, and threads release locks.
Sleep : The thread releases execution, but does not release the lock.
Java Basics Point Grooming 2