JAVA learning lesson 21st (multithreading (I) preliminary understanding)
I had a holiday at home. I had a rest for several days and didn't learn it. Today I want to learn about multithreading and find my feeling. I will go back to school the day after tomorrow, sad...
PS: the package has no technical content and can be used. It will be used for development in the future. So I plan to release it first, and multithreading first.
I. multithreading Overview
What is a process?
In layman's terms, it can be understood as a program in progress. A process is actually the space of an application in the memory.
The process is not executed directly. The process only allocates the memory space required by the application. The thread is responsible for execution. The thread is responsible for a control unit for content execution in the process, also known as the execution path, it is also called an execution scenario.
What is a thread?
A thread is a control unit (Execution path) in a process that is responsible for program execution)
A process can have multiple execution paths, called multithreading.
For example, in 360, the computer health check is running while the garbage cleaning is also running.
There must be at least one thread in a process.
Multiple Threads are enabled to run code of multiple parts at the same time.
Each thread has its own running content, which can be called the task to be executed by the thread.
Ii. Advantages and Disadvantages of Multithreading
It seems that multithreading technology is efficient, but there are drawbacks.
Windows itself is an operating system that runs multiple tasks at the same time. For example, if Win is running QQ and 360, it seems that he is chatting with QQ and 360 while scanning, but they are simultaneously. In fact, they are not, the CPU is responsible for program execution. At a certain point in time, the CPU is controlling the running of QQ. At this point, 360 won't run, and the switching speed of CPU is very fast, so they feel that they are running at the same time, in fact, at a certain time point, only one program is running, that is, only one path is being executed. Therefore, when many applications are opened, the program will be compared, A single program gets less CPU execution frequency (PS: CPU switching is random and depends on time slice)
Benefits: solves the problem of multiple concurrent operations.
Disadvantages: too many threads will reduce the efficiency
Therefore, multithreading technology can solve the problem of simultaneous running of multiple programs, but when the program is opened too much, it will be inefficient, or even crash.
Of course, even if the CPU is very powerful but the memory space is insufficient, it cannot be quickly switched.
Iii. multithreading in JVM
Virtual Machine startup depends on multithreading.
The JVM starts multiple threads at startup. There are at least two threads.
1. the thread that executes the main function (main thread)
The task code of this thread is defined in the main function.
2. Garbage collection thread (garbage collection thread)
When the main thread controls the execution of main, there are multiple spam in the heap memory, and another thread controls the garbage collection.
Example |: the heap memory generates many objects. Each object may be recycled, and each object may be different. Underlying resources are used, but not used, only objects are clear about how objects are recycled, so every object has a method that can be recycled. The methods that every object has are defined in the Objectt class, called finalize, purpose: When the Garbage Collector determines that there is no more reference to this object, the object's garbage collector calls this method. Subclass RewritingfinalizeTo configure system resources or perform other cleanup operations.
Class Demo extends Object {public void finalize () {System. out. println ("Demo. finalize ") ;}} public class Main {public static void main (String [] args) {new Demo (); System. gc (); // start the garbage collector System. out. println ("hello word ");}}
PS: when the main thread is executed to start the garbage collector, the garbage collection thread starts and the main thread continues to run downward. Therefore, the helloword is printed first, and the main thread ends, but other threads are still running.
4. main thread running instance
Heap memory is not recycled when garbage is generated, because it will compete with the main thread for the CPU execution right, so who gets the execution right at a certain time, who executes
Main thread-> aDemo. show () into the stack-> execute the exit stack-> bDemo. show () into the stack-> execute the exit stack-> return <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3ryb25np1_vcd4kpha + release + CjxwcmUgY2xhc3M9 "brush: java;"> class Demo {private String name; public Demo (String name) {// TODO Auto-generated constructor stubthis. name = name;} public void show () {for (int I = 0; I <10; I ++) {for (int j =-9999999; j <9999999; j ++) {} System. out. println (name + "I =" + I) ;}} public class Main {public static void main (String [] args) {Demo aDemo = new Demo ("A"); Demo bDemo = new Demo ("B"); aDemo. show (); bDemo. show (); System. out. println ("hello word ");}}
In this way, the program will be slow, and A will not be able to execute B after it is executed. How to Implement simultaneous execution of A and B requires that A be implemented in A path, B is in A path and the CPU is switched, so that A and B can run simultaneously.
This involves the creation of multiple threads.