(Copyright statement: original works, please do not forward! Otherwise, legal responsibility will be pursued.
Program this season
In this part of multithreading, focus on mastering the following points (concepts):
The difference between thread and process
The implementation method and difference of multi-threading in java
· Java thread synchronization and deadlock
The basic concept of multithreading (1)
DOS started: If a virus is present, the operating system will die, because DOS can only have one program running at the same time.
Windows: Is the operating system still usable if a virus is present?
But how many CPUs does the computer have? One? Multi-process is to divide the resources of the CPU. There will be multiple programs running at the same time period, but only one program can run at the same time point.
Process and thread connection?
An operating system can have multiple processes
Each process is divided into multiple threads
If the process disappears, the thread disappears
If the thread disappears, the process may not disappear.
Basic concepts of multithreading (2)
Multithreading itself still requires operating system support. That is to say, using multithreading will provide resource utilization.
The realization of JAVA thread
The program executes thread A, thread B, and thread C in sequence, as if there are multiple programs running at the same time period.
The above code is called through the run method, and the multi-threading mechanism is not started. In fact, pay attention to one point: each operating system has its own resource division, that is to say, if you want to implement a multi-threaded program on an operating system, you must not directly call run (the method in the class) to start, but should notify The operating system is assigned by the operating system.
If you want to start multiple threads, you must call the start() method, and the start method can call the run method overridden by the subclass
Let's now see if the program can start multithreading
The run method is indeed called through the start() method, and the programs at the moment are indeed running together, and they are running alternately. Whoever grabs the CPU resources will run.
Why do you have to call the start() method to start it?
Definition of Thread class in JDK
Among them, started is to define a boolean variable in the method to determine whether the thread has been started. If it is not started, this code is executed, indicating that the thread has been started, the thread participation is properly arranged, and the private method start0 is called. This method Indicates that a function of the operating system is to be called.
native is a keyword
There is a technology in JAVA: JNI (JavaNativeInterface), which can use JAVA program to call the functions of the native operating system to achieve certain functions.
What if an already started thread is started again?
The program will throw an IllegalThreadStateException() exception
Remember: as long as the thread is started once, it is OK.
Is there actually a problem with the above procedure?
If it is necessary for a class to inherit the Thread class to realize multi-threading, this class must not inherit other classes, that is, if Thread is used, it will be limited by single inheritance, so the implementation of multi-threading usually implements the Runnable interface.
At the structure method of the Thread class:
publicThread(Runnabletarget)--gt; indicates that as long as it is a subclass target of the Runnable interface, it can be passed to it.
Verify the effect, multi-threading does work~
Conclusion: No matter which method implements multithreading, it will always start multithreading through the Thread class.
Thread class and Runnable interface (1)
The Thread class also implements the Runnable interface~
Thread class and Runnable interface (2)
1. Preventing single inheritance restrictions
2. Resource sharing that can reach multiple threads
Verify it works~
We found that there is a problem with this program. The three threads each accounted for 10 tickets, and a total of 30 tickets were sold. Why is this? Because of the privateintticket=10; attribute, once the following code is implemented:
MyThreadmt1=newMyThread();
MyThreadmt2=newMyThread();
MyThreadmt3=newMyThread();
After the implementation, the instantiation operation is realized, and mt1, mt2, and mt3 each occupy their own attributes, that is, each occupies 10 tickets, so that resource sharing is not achieved.
If we want to achieve resource sharing, we can modify the above program, see below~
Now the three targets have sold 10 tickets in total, which has reached the role of resource sharing ha~~~
If the Runnable interface is used to implement multithreading, then all thread objects occupy the same subclass object of the Runnable interface, which proves that multiple threads share resources.
Conclusion: It is best for a class to implement the Runnable interface to implement multithreading.
Summarize
1. The difference between process and thread
Threads are divided on a process basis
Processes are divided on the operating system
2. Using multi-threading, it can be found that all programs on the same time period are running alternately.
3. There are two ways to implement multi-threading:
Implement the Runnable interface
·Inherit the Thread class
4. Connection and difference:
The Thread class is also a subclass of the Runnable interface
If you want to start a thread, it is necessary to put a custom subclass target that implements the Runnable interface into the Thread class
The Runnable interface can realize the sharing of multiple resources
·It is absolutely necessary to use the Runnable interface first to prevent the limitation of single inheritance.
#################################################)