Process: a running program is called as a process. The process is responsible for dividing the memory space
Windows claims to be a multitasking operating system, so does Windows run multiple applications at the same time?
Macro Perspective: Windows is really running multiple applications at the same time
Micro-angle: The CPU is doing a quick switch to perform the action, because the speed is too fast, so we do not feel the switch only
Single-core CPUs can only execute one application in one time slice
Each application is actually in the battle for CPU Resources.
Threads: responsible for execution of code
Multithreading: There are multiple threads in a process that perform different tasks
The thread is responsible for the execution of the code, there was no thread before, why the code can be moved to execute?
Any Java program, the JVM will create a main thread to execute all the methods in the main method when it runs
How many threads does a Java application have at least?
There are at least two threads, one is the main thread, the main method is responsible for the execution of things, one is the garbage collector, responsible for garbage Collection.
Rather than a process in the CPU of the resource scramble, rather, the thread is doing the Cpu's resource scramble
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 use of resources will not be exhausted)
Disadvantages:
Increases the burden on the CPU
Reduce the execution probability of threads in a process
Can cause thread safety issues
There is a deadlock phenomenon
How to create Multithreading:
How to create a thread:
Way One:
1. Customizing a class to inherit the tread class
2. Overriding the Run method of the thread class
Question: What is the purpose of overriding the run method?
Each thread has its own task code, and the task code of the main thread that the JVM creates 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 of thread and call the Start method to open the thread
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 directly is the equivalent of calling a common method.
The main thread and the custom thread are alternately
The life cycle of a thread:
Waiting for the CPU to qualify
CPU Execution rights
Create state (when new out of thread class object): no CPU waiting, no CPU execution
(call the start METHOD)
Operational status (with CPU waiting, no CPU Execution)
(get CPU Execution right, If the CPU is robbed of execution, return to the previous State)
Running state (with CPU execution right, CPU Waiting)
(complete Task)
Death status (no CPU wait qualification, no CPU Execution)
Temporary blocking state (in the running state of the thread, once sleep or WAITFANGFA is executed, then the thread will go into a temporary blocking state, if the thread is called sleep into the blocking state, then once the thread has exceeded the specified sleep time, it will re-enter the operational state, If the wait method is called to enter a temporary blocking state, then the thread is required to wake up another thread to re-enter the operational State) without waiting for eligibility
Common Methods for threading
Thread (String name); The name of the initialization thread
GetName () Returns the name of the thread
SetName (String Name) Setting set Thread object name
Sleep () The number of milliseconds that a thread sleeps specifies is a static method which thread executes the sleep method, then which thread sleeps
GetPriority () Returns the priority of the current thread for the default thread priority is 5
SetPriority (int Newpriority) Sets the priority of the thread, although the priority of the thread is set, but the specific implementation depends on the implementation of the underlying operating system (the maximum priority is 10, the minimum is 1, the default is 5)
CurrentThread () Returns the thread object being executed by the CPU the method is a static method, note: which thread executes the CurrentThread () code, returns the object of that thread
Why can't the Run method of the thread subclass throw an exception?
Because the thread Class's Run method does not throw an exception, the subclass that inherits the parent class cannot throw an exception, the Run method is overridden, and the overridden method throws an exception that is less than or equal to the exception thrown by the parent class
Non-static member variables, non-static member variable data is the one that maintains a copy of the data in each Object.
Under what circumstances will a thread safety issue Occur:
1. There are two or more threads, and a data resource is shared between threads
2. have multiple statements to manipulate shared resources
Benefits of Multithreading:
1. Resolves an issue in which multiple tasks can be performed concurrently in a process
2. Increased utilization of resources
Disadvantages of Multithreading:
1. Increase the burden on the CPU
2. Reduces the execution efficiency of a thread in a process
3. A thread-safety issue has occurred
4. A deadlock behavior is triggered
A solution to the thread safety problem: Sun provides a thread-synchronization mechanism that lets us solve these problems
How the Java thread synchronization mechanism works:
Mode one: Synchronizing Code blocks:
Format of the Synchronized code block:
Synchronized (lock Object) {
Code that needs to be synchronized
}
Things to keep in mind when synchronizing code blocks:
1. The lock object can be an arbitrary object,
Any object can be used as a lock object. A state is maintained within the object, and the Java synchronization mechanism uses the state in the object as the identity of the lock (which can be locked with a string).
2. Calling the sleep method in a synchronous code block does not release the lock object
3. Use synchronous code blocks only if there is a real thread safety problem, otherwise it will reduce efficiency
4. The lock object for multi-threaded operations must be unique and shared, otherwise invalid
Mode Two: the synchronization function uses synchronized to modify a function
Considerations for Synchronization Functions:
1. If the lock object that is a non-static synchronization function is the This object if the lock object of a static synchronization function is a bytecode file (class Object) of the class to which the current function belongs, (after creating an object in the method area, the system decomposes the corresponding class, and the resulting method attributes are encapsulated on a class Object)
Recommended Use: synchronizing 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.
A synchronization mechanism in Java that resolves security issues in threads and also causes deadlocks
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 Scenario: no scheme, only try to avoid the occurrence
How to create a custom thread two:
1. Customizing 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 the object of the thread class and pass the object of the Runnable implementation class as a parameter
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? -------thread must have a Start method
The object of the Runnable implementation class is not a thread object, but an object that implements the Runnable Interface.
Only the thread and thread objects belong to the threading object
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.
The creation of custom threads is recommended for the second Type. Implementation of the Runnable interface
Reason: because Java is a single-inheritance multi-implementation
Thread _ Basic