1th ChapterMultithreading1.1Multithreading Introduction
Before we learn multithreading, we need to know a couple of concepts about multithreading.
Process: A process refers to a program that is running. To be exact, when a program goes into memory, it becomes a process, and the process is a program that is in the process of running and has a certain independent function.
Thread: A thread is an execution unit in a process that is responsible for the execution of a program in the current process, with at least one thread in a process. There can be multiple threads in a process, and this application can also be called a multithreaded program.
In short: After a program runs at least one process, a process can contain multiple threads
What is multithreading? That is, there are multiple threads executing at the same time in a program.
The difference between a single-threaded program and a multithreaded application is distinguished by:
L Single-threaded: that is, if more than one task can be executed sequentially. When the previous task finishes executing, the next task begins. For example, to Internet cafes, cafes can only let a person online, when this person under the machine, the next person to the Internet.
Multi-threaded: That is, if there are multiple tasks that can be executed simultaneously. For example, to Internet cafes, cafes can allow many people at the same time Internet.
1.2Program Operating principle
L Time- sharing scheduling
all threads take turns using CPU usage, with an average allocation of CPU time per thread.
L preemptive scheduling
prioritize high-priority threads to use CPU, if the priority of the thread is the same, then randomly select one ( thread randomness ),Java is used for preemptive scheduling.
1.2.1preemptive scheduling detailed
most operating systems support multi-process concurrency, and today almost all operating systems support running multiple programs at the same time. For example: Now we use the editor in class, while using the screen software, but also open the drawing board,dos windows and other software. At this point, these programs are running at the same time," feeling like the software is running at the same time ."
in fact, The CPU ( central processing unit ) uses preemptive scheduling mode to switch between multiple threads at high speed. For One core of the CPU, at some point, only one thread can be executed, and the CPU switches between multiple threads faster than we feel, and looks like it is running at the same time.
In fact, multi-thread programs can not improve the speed of the program, but can improve the efficiency of the program, so that CPU Utilization is higher.
1.3Main Thread
recall the code we used to learn, when we entered the Java Space class name in the dos command line carriage return, start the JVM, and load the corresponding class file. The virtual machine starts executing our program code from the main method and ends the code execution of the main method. If the execution process encounters a long cycle of code, the other code after the loop will not be executed immediately.
classdemo{String name; Demo (String name) { This. Name =name; } voidShow () { for(inti=1;i<=10000; i++) {System.out.println ("Name=" +name+ ", i=" +i); } }}classThreaddemo { Public Static voidMain (string[] args) {Demo d=NewDemo ("Xiao Qiang"); Demo D2=NewDemo ("Wang Choi"); D.show (); D2.show (); System.out.println ("Hello world!"); }}
If the number of loops in the Show method is executed in the code above , then in d.show (); The following code is not executed immediately, and the dos the window will see a constant output. name= Xiao Qiang , i= value, such as a statement. Why is that?
The reasons are: after the JVM is started, there must be an execution path ( thread ) starting from the main method and executing to The main method ends and this thread is called the primary thread in Java . when the main thread of the program executes, if it encounters a loop that causes the program to stay too long at the specified location, the following program cannot be executed immediately, and it needs to wait for the loop to finish .
So, can you implement a main thread that executes one of the loops, and then another thread is responsible for executing the other code, ultimately implementing the effect of the multi-part code execution concurrently?
can be implemented simultaneously, by multithreading technology in Java to solve this problem.
1.4Thread class
How do I create a thread? Search through the API to find the Thread class. By reading The description in the Thread class. thread is the thread of execution in the program. a Java virtual machine allows an application to run multiple execution threads concurrently.
There are two ways to create a new thread of execution.
L One method is to declare a class as a subclass of Thread. The subclass should override the Run method of the Thread class. creating objects, opening threads . the Run method is equivalent to the main method of another thread.
Another way is to declare a class that implements the Runnable interface. The class then implements the Run method. It then creates a subclass object of Runnable, which is passed into the constructor method of a thread , and the thread is opened.
1.5 Create thread mode one inherits the thread class
Steps to create a thread:
1 defines a class that inherits the thread.
2 override the Run method.
3 Creating a Subclass object is creating a thread object.
4 Call the start method, open the thread and let the thread execute it, and tell the JVM to call the run method
Java Basics-Threading