Process: The program being executed as a process. The process is responsible for dividing the memory space
Problem: Windows claims to be a multitasking operating system, so is windows a colleague running multiple programs?
From a macro perspective: Windows does actually run multiple programs at the same time
From the microscopic point of view: The CPU is doing a quick switch to perform the action, because of the speed, we do not feel the switch only
Threads: Threads are responsible for the execution of code in a process, which is an execution path in a process
Multithreading: There are multiple threads in a process that perform different tasks at the same time.
Question: The thread is responsible for the execution of the code, we have not learned the thread before, why the code can execute it?
Running any Java program, the JVM will create a main thread to execute all the code in the main method when it runs
A Java at least a few threads?
There are at least two threads, one is the main thread responsible for the execution of the Main method code, one is the garbage collector thread, responsible for garbage collection
Benefits of Multithreading:
1. Resolves an issue in which a process can perform multiple tasks at the same time
2. Increased utilization of resources
The drawbacks of multithreading
1. Increase the burden on the CPU
2. Reduces the execution probability of a thread in a process
3. Thread-safety issues are raised
4. There is a deadlock phenomenon
How to create multithreading
Way One:
1. Customize a class to inherit the thread class.
2. Rewrite the thread class's Run method to write the task code of the custom thread in the Run method
Question: What is the purpose of overriding the Run method?
Each thread has its own task code, the task code of the main thread created by the JVM is all the code in the Main method, and the custom thread's task code is written in the Run method, and the custom thread is responsible for the code in the Run method.
3. Create a child class object for thread, and call the Start method to open the thread.
Note: Once a thread is turned on, the thread executes the code in the Run method, and the run method must not be called directly, and calling the Run method calls a normal method rather than opening a new thread.
Thread safety issues:
The root cause of thread safety occurs:
1. There are two or more thread objects that share the same resource
2. Multi-threaded operation shared resources code has multiple offices
Solutions for thread safety issues:
Method: You can use the synchronization code block to solve
Format:
Synchronized (lock object) {
Code that needs to be synchronized
}
Things to keep in mind when synchronizing code blocks:
1. The lock object can make any one object
2. A thread sleep in a synchronized code block and does not release the lock object.
3. If there is no thread safety problem, do not use synchronous code blocks, which will reduce efficiency
4. The lock object must be a resource for multi-threaded sharing, or it cannot be locked
Mode two: Synchronization function: The synchronization function is to use synchronized to modify a function. Things to note about synchronization functions
1. If it is a non-static synchronization function lock, the object is the this object if it is a static synchronization function of the lock, the object is the current function belongs to the class of the bytecode file (class object)
2. The lock object of the synchronization function is fixed
Recommended use: Synchronize code blocks.
Reason:
1. The lock object of the synchronization code block can be specified by us at will, which is convenient to control. The lock object of the synchronization function is fixed and cannot be specified by us.
2. Synchronizing code blocks makes it easy to control the range of code that needs to be synchronized, and the synchronization function must be all the code for the entire function to be synchronized.
There is a thread-safety problem with synchronization mechanism in Java, but it is also a deadlock-causing phenomenon
Deadlock phenomenon:
The root cause of the deadlock phenomenon:
1. There are two or more than two threads
2. There are two or more than two shared resources
Deadlock resolution: There is no plan, only try to avoid the problem.
Customize how threads are created:
Way One:
1. Customize a class to inherit the thread class.
2. Rewrite the thread class's Run method to write the task code of the custom thread on the Run method.
3. Create a child class object for thread, and call the Start method to start a thread.
Note: Never call the Run method directly, the thread will open when the Start method is called, and the thread will execute the code in the Run method once it is opened, if it is called directly
Run method, then it is equivalent to calling an ordinary method.
Way two:
1. Customize a class to implement a runnable interface.
2. Implement the Run method of the Runnable interface to define the task of the custom thread on the Run method.
3. Create the Runnable implementation class object.
4. Create an object of the thread class and pass the object of the Runnable implementation class as an argument.
5. Call the Start method of the thread object to open a thread.
Question 1: Is the object of the Runnable implementation Class A thread object?
The object of the Runnable implementation class is not a thread object, but an object that implements the Runnable interface.
Only threads or thread subclasses are thread objects.
Question 2: Why should the object of the Runnable implementation class be passed as an argument to the thread object? What is the role?
The function is to execute the Run method of the object of the Runnable implementation class as the thread's task code.
Recommended use: The second type. Implement the Runable interface.
Reason: Because Java is single-inheritance, multi-implementation.
package com.yuanzijian03;public class demo03 implements runnable{@Overridepublic void run () {/*system.out.println ("this:" + this); System.out.println ("Current thread:" + thread.currentthread ()); */for (int i = 0 ; i < 100 ; i++) {System.out.println (Thread.CurrentThread (). GetName () + ":" +i);}} Public static void main (String[] args) {//Create an object for the Runnable implementation class demo03 d = New demo03 ();//Create an object of the thread class, pass the Runnable implementation class object as an argument. Thread thread = new thread (D, "Dog Doll"); the //thread class uses the target variable to record the D object,// Call the Start method of the thread object to open the thread. Thread.Start (); for (int i = 0 ; i < 100 ; i++) { System.out.println (Thread.CurrentThread (). GetName () + ":" +i);}} /* thread class Run method * @Override public void run () { if (target != null) { target.run (); //is equivalent to the runnable implementation of the object's Run method as the thread object's task code. } }*/}
Execution results
main:0
Dog Doll: 0
Main:1
Dog Doll: 1
。。。。。
Main:98
main:99
Java Multi-Threading