Java multithreading Series 1-thread implementation and scheduling, java multithreading 1-Thread
One of the important functions of java is its internal support for multithreading. This series of articles will detail the basic knowledge of java multithreading.
Multithreading Overview
Multithreading Introduction
- The program has only one execution flow, so such a program is a single-threaded program.
- If a program has multiple execution flows, the program is a multi-threaded program.
Process: A running program is an independent unit for the system to allocate and call resources. Each process has its own memory space and system resources.
Thread: A single sequential control flow in a process and an execution path. If a process has only one execution path, it is called a single-threaded program.
A process with multiple execution paths is called a multi-threaded program.
Java program running principle
The java command will start the java Virtual Machine and start the JVM. It is equivalent to starting an application, that is, starting a process. This process automatically starts a "main thread" and then the main thread calls the main method of a class. Therefore, the main method runs in the main thread. Before that, all programs were single-threaded.
The Java Virtual Machine is multi-threaded, because apart from the main thread, there are also garbage collection threads
Multi-thread Implementation Scheme
Method 1: Inherit the Thread class
Procedure
1. The custom class MyThread inherits the Thread class.
2. Rewrite run () in the MyThread class ()
3. Create an object
4. Start the thread
The following code:
/** This class needs to override the run () method. Why? * Not all code in the class needs to be executed by the thread. * In this case, java provides run () in the Thread class to distinguish which code can be executed by the Thread. */Public class MyThread extends Thread {@ Override public void run () {// generally, the Code executed by the Thread must be time-consuming. So we use a loop to improve for (int x = 0; x <300; x ++) {System. out. println (x) ;}} public class MyThreadDemo {public static void main (String [] args) {// create two thread objects MyThread my1 = new MyThread (); myThread my2 = new MyThread (); my1.start (); my2.start ();}}
Steps:
1. Customize the class MyRunnable to implement the Runnable interface
2. Rewrite the run () method.
3. Create an object of the MyRunnable class
4. Create a Thread Class Object and pass the object in Step C as the construction parameter.
Public class MyRunnable implements Runnable {@ Override public void run () {for (int x = 0; x <100; x ++) {// The Thread class method cannot be used directly because of the interface implementation method, but the System can be used indirectly. out. println (Thread. currentThread (). getName () + ":" + x) ;}}/** Method 2: implement the Runnable interface * Step: * A: implement the Runnable interface of the custom class MyRunnable * B: override the run () method * C: Create the object of the MyRunnable class * D: Create the object of the Thread class, and pass the object in Step C as the construction parameter */public class MyRunnableDemo {public static void main (String [] args) {// create MyRunnable Class Object MyRunnable my = new MyRunnable (); // create a Thread class object, and pass the object of Step C as the construction parameter // Thread (Runnable target) Thread t1 = new Thread (my); Thread t2 = new Thread (my ); t1.setName ("zhangsan"); t2.setName ("lisi"); // Thread (Runnable target, String name) Thread t1 = new Thread (my, "zhangsan "); thread t2 = new Thread (my, "lisi"); t1.start (); t2.start ();}}
Benefits of implementing the interface:
1. Avoid the limitations caused by Java single inheritance.
2. It is suitable for the code of multiple identical programs to process the same resource, and effectively separates the code and data of the same program from each other, which better reflects the object-oriented design idea.
Get and set thread name
Basic methods for obtaining and setting Thread classes:
Public final String getName () public final void setName (String name)
In fact, you can also name the thread through the constructor.
How can I get the name of the thread where the main method is located?
Public static Thread currentThread ()
In this way, the thread name of any method can be obtained.
The sample code is as follows:
Public class MyThread extends Thread {public MyThread () {} public MyThread (String name) {super (name) ;}@ Override public void run () {for (int x = 0; x <100; x ++) {System. out. println (getName () + ":" + x) ;}} public class MyThreadDemo {public static void main (String [] args) {// create a thread object // No parameter construction + setXxx () MyThread my1 = new MyThread (); MyThread my2 = new MyThread (); // call the method to set the name my1.setName ("zhangs An "); my2.setName (" lisi "); my1.start (); my2.start (); // name MyThread my1 = new MyThread (" zhangsan ") for the thread with parameters "); myThread my2 = new MyThread ("lisi"); my1.start (); my2.start (); // What should I do to get the name of the thread object where the main method is located? // Public static Thread currentThread (): returns the currently executing Thread object System. out. println (Thread. currentThread (). getName ());}}
Thread Scheduling
If our computer has only one CPU, the CPU can only execute one command at a certain time. The thread can execute commands only when it obtains the CPU time slice, that is, the right to use it. So how does Java call the thread?
There are two scheduling models for threads:
- In the time-sharing scheduling model, all threads use the CPU in turn, and the time slice used by each thread is evenly allocated.
- The preemptible scheduling model gives priority to CPU usage by high-priority threads. If the threads have the same priority, one thread will be randomly selected. The threads with higher priority will obtain more CPU time slices.
Java uses a preemptible scheduling model.
Public final int getPriority () public final void setPriority (int newPriority)
The sample code is as follows:
Public class ThreadPriority extends Thread {@ Override public void run () {for (int x = 0; x <100; x ++) {System. out. println (getName () + ":" + x) ;}}/ ** public final int getPriority (): returns the priority of the thread object * how to set the priority of the thread object? * Public final void setPriority (int newPriority): Change the thread priority. ** Note: * The default thread priority is 5. * The thread priority ranges from 1 to 10. * A high thread priority only indicates that the CPU time slice obtained by the thread has a high probability, but the effect can be seen only when the number of CPU time slice is large or multiple times are run. * **/Public class ThreadPriorityDemo {public static void main (String [] args) {ThreadPriority tp1 = new ThreadPriority (); ThreadPriority tp2 = new ThreadPriority (); threadPriority tp3 = new ThreadPriority (); tp1.setName ("zhangsan"); tp2.setName ("lisi"); tp3.setName ("wangwu"); // obtain the default priority System. out. println (tp1.getPriority (); System. out. println (tp2.getPriority (); System. out. println (tp3.getPriority (); // sets the thread priority // tp1.setPriority (100000); // sets the correct thread priority tp1.setPriority (10); tp2.setPriority (1); tp1.start (); tp2.start (); tp3.start ();}}