1. Introduction
Multithreading technology belongs to the knowledge of the operating system scope;
Processes and Threads
As you can see, an application is a process that contains at least one thread in a process, and the process is the container for the thread, and the thread that really works and processes the task.
The process is the basic unit of the operating system allocating resources, the thread is the basic unit of the operating system scheduling and time allocation;
The process consists of the kernel object and the address space, the kernel object is a small piece of memory to record the process information, only allow the operating system access; Address space is the space for storing data and programs;
2. Multi-threaded operation mechanism
For a single CPU, only one thread can be executed at each point in time, and the implementation of multithreading is based on the samsara mechanism of the time slice, which is to allocate a very short time slice for each thread; the time slice ends with the execution of another thread, and the application cannot change when the time slice is allocated by the operating system. The application can set the priority of the thread.
With regard to the priority of threads, for Windows operating systems, the priority of a thread is determined based on the priority class of the process and the relative priority class of the thread, method
Note that the process priority is generally not set to real-time and will be problematic.
Then say the priority, the higher the priority, the longer the allotted time slices, the precedence is not the same as the execution order, the execution order of the threads is affected by many factors of the operating system, the priority is only one of the factors, so it is unrealistic to expect to take precedence to control the order of execution of threads.
In addition, different languages have different priorities, and the priority is appropriate for the operating system and platform-related, the use of priority control programs, in one platform to run well, and in another platform may be problematic.
3. Thread life cycle (based on Java language)
New (Create Thread), Run/start (execution thread), block (blocking thread), wait (release lock, wait for Wake), Destroy (death)
Use start for run and start as much as possible, because although run is the thread's real handler, calling run is the equivalent of calling a method, run does not end, does not execute the subsequent statement, and call start automatically calls run, and whether or not the run is complete, Will execute the subsequent program, which is the real sense to start a thread;
4, based on Java Multi-threading implementation
There are two ways to implement multithreading based on Java, one is to inherit the thread class and the other is to inherit the Runnable interface, neither of which is inherently different because the thread class itself inherits the Runnable interface, but only in some encapsulation. The two approaches are differentiated by use, and if a class already inherits other classes, it can no longer integrate the thread class, so only the second method can be used.
The class that inherits from the thread class can call the method of thread directly;
That's probably it.
Public class testThread1 extend thread{ /** * Rewrite the run method */public void Run () { } }// call mode testThread1 new// Automatically execute the Run method
Way One
Classes that inherit from the Runnable interface must be instantiated and then initialize a thread object with the instantiated object;
That's probably it.
public class testThread2 implements runnable{ /** * Override the Run method */ public void Run () {}} //
call mode testThread2 threadinstance =
new
TestThread2 (); Thread real_threainstance =
new
thread ( threadinstance); real_threainstance. Start ();
//
automatically executes the Run method
Mode two
Essentially, the runnable run method is overridden, and the function in run is the function that the thread executes.
5. Multi-threaded communication based on Java
The communication between multiple threads is classified into three types: (1) shared variable (2) message mechanism (3) pipeline mechanism (temporarily not known yet)
(1) Shared variables
Each thread shares the address space of the process, so it can access the public address space in the process so that the variables in the public address space can be accessed by each thread, and if the address space in the process is assigned to a thread, the other threads are not accessible.
(2) Message mechanism
Mainly introduces the application of sleep join wait notify synchronized
Synchronized: To lock the Obj object, call method synchronized (obj) {}//obj is an object as long as it is inherited from the objects class, such as: String type in Java, each inherits from object The object of the class has a lock, and when the object is chained to it, the other thread will not be able to access the object again;
Sleep: Causes the thread to hibernate for a certain amount of time, not releasing the lock during hibernation
Wait: There are two ways to call Wait (1000)//Specify Sleep Time Wait ()//Do not specify sleep time, the lock is released during hibernation, and wait () must be placed in the synchronized code block, because only after locking can release the lock and release the Synchronized (obj) obj lock; otherwise it will be reported as abnormal;
Notify: Wake up a wait thread
Multithreading technology based on Java language