Concept of Multithreading
I,ProgramAnd Process
1. Program: A piece of staticCode.
2. process: a dynamic execution process of a program, which corresponds to a complete process from code loading, execution to execution.
3. A process is also called a task. an OS that supports simultaneous execution of multiple processes is called a multi-process OS or multi-task OS.
2. Process and thread
In a program, multiple tasks can be concurrently executed. Each task is called a thread.
A thread is a smaller execution unit than a process. It is an independent control flow in a process, that is, the control flow inside the program.
Features: The thread cannot run independently and must depend on the process to run in the process.
Each program has at least one thread called the main thread.
Single thread: a process with only one thread is called a single thread.
Multithreading: a process with more than one thread is called multithreading.
Iii. Advantages and Disadvantages of Multithreading
Improve the response speed of the interface program. By using threads, you can start separate threads in the background for a large amount of time to complete the process, improving the corresponding speed of the front-end interface.
Make full use of system resources to improve efficiency. By executing multiple processes within a program at the same time, you can make full use of CPU and other system resources to maximize hardware performance.
When the number of threads in a program is large, the system will spend a lot of time switching threads, which will reduce the execution efficiency of the program. However, compared with the advantages, the disadvantages are still very limited. Therefore, the multi-threaded programming technology has been widely used in current project development.
Multithreading
1. When implementing Thread Programming, you first need to make a class have the ability of multithreading, inherit the Thread class or implement the runnable interface class with the ability of multithreading, and then create thread objects, you can call the corresponding startup thread method to implement thread programming.
Multiple Threads can be implemented in a program. multithreading means that two or more threads are started in the same program.
Java provides three implementation methods in actual implementation threads:
(1) inherit the Thread class
(2) Implement the runnable interface
(3) Use timer and timertask combination
Ii. inherit the Thread class to implement Multithreading
The Java. lang Package provides a special thread class, which encapsulates many methods for Thread Scheduling and processing. If a class inherits the Thread class, the class can be executed in multiple threads.
1 Class Mythread Extends Thread { 2 3 Public Void Run (){ 4 5 // Thread body 6 7 } 8 9 } 10 11 Inherit the Thread class to implement multithreading. 12 13 Mythread TT1 = New Mythread (); 14 15 // Start thread 16 17 Tt1.start (); 18 19 Try { 20 21 For ( Int I = 0; I <5; I ++ ){ 22 23 // 1 second delay 24 25 Thread. Sleep (1000 ); 26 27 System. Out. println ("Main:" + I ); 28 29 } 30 31 } Catch (Exception e ){}
Note:
Thread features: Random. When the system executes a multi-threaded program, it only ensures that the thread is executed alternately. As to which thread executes the program first and then executes the program, it cannot be guaranteed, the execution sequence can be ensured only by writing special code.
Multiple Threads can be started for the same thread class.
The same thread cannot be started twice,
When the run method in the Custom thread is completed, the custom thread will naturally die. For the system thread, it will end only after the main method is executed and other started threads are completed. After the execution of the system thread is completed, the execution of the program is actually completed.
3. Implement the runable Interface
A) multi-threaded objects implement the java. Lang. runnable interface and rewrite the run method of the runnable interface in this class.
B) Benefits: The method for implementing the runable interface avoids the limitation of single inheritance.
Example 1: Use the runable interface to implement multiple threads.
1 Class Mythread2 Implements Runable { 2 3 Public Void Run (){} // Override the run () method in the runable Interface 4 5 } 6 7 Mythread2 MT1 = New Mythread2 (); 8 9 Thread T1 = New Thread (MT1 ); 10 11 T1.start ();
Thread Lifecycle
I. Thread Lifecycle
A thread is a process of dynamic execution. It also has a process from generation to death, which is called a lifecycle. A thread has five states in its lifecycle:
1. New thread)
When an instance (object) of the thread class is created, the thread enters the new State (not started ).
Example: thread T1 = new thread ();
2. Ready (runnable)
The thread has been started and is waiting for being allocated to the CPU time slice. That is to say, the thread is waiting in queue to get CPU resources. Example: t1.start ();
3. Running)
The thread obtains the CPU resources and is executing the task (run () method). At this time, the thread will continue to run until it automatically gives up CPU resources or has a higher priority thread to enter.
4. Dead)
When the thread execution is completed or killed by other threads, the thread enters the dead state. At this time, the thread cannot enter the ready state and wait for execution.
Natural termination: terminate after running the run () method normally
Exceptional termination: Call the stop () method to terminate a thread.
5. Blocked)
For some reason, the running thread gets out of the CPU and suspends its execution, that is, it enters the blocking status.
Sleep: use sleep (long T) to enable the thread to sleep. A sleeping thread enters the ready state at a specified time.
Waiting: Call the wait () method. (Call the motify () method to return to the ready state)
Blocked by another thread: Call the suspend () method. (Call resume () to restore)