Multithreading:
The effect of implementing simultaneous execution is that multiple threads are actually scrambling for CPU
Enables simultaneous execution of different functions
Multithreading does not necessarily improve efficiency, but it is possible to use PCU resources reasonably.
Multi-threaded running results are different because threads are scrambling for CPUs, which is the randomness of multithreading
Concept:
Progress (Process):
Each application corresponds to a process
Running programs, that is, memory space that is opened up in memory
Threads (thread):
An execution path that is responsible for program execution, also known as an execution unit
The execution of a process is actually a thread executing
A process will have at least one thread (main thread), and the main thread executes the main function code
When there are multiple threads in a process, it is a multi-threaded
Tasks (Task):
Code that each thread needs to execute
The task code has its storage location
The main thread of the task code in the main function, the garbage collection thread's task code in the Finalize function
The thread is present as the task exists and disappears as the task ends
/**
*
* is the JVM (Java virtual machine) multithreaded?
*
* There is at least one thread responsible for normal execution, that is, executing the code in the main function----the main thread
* There is also a thread responsible for garbage collection, that is, executing code in the Finalize function----garbage collection thread
*
* * * Each object can be recycled, the function of recycling is defined in the Finalize () method of Object
* * * Run the garbage collector, call the system's GC () method
*
**/
The first way to create a thread:
1 Create a class to inherit the thread
2 overriding the Run method in the thread class
Threads are created to perform tasks
The task code must have a storage location, and the Run method is where the task code is stored
3 Creating a subclass object, actually creating a thread
4 Start thread
The main thread's task code is in the main function
The task code for the child thread is in the Run function
The **run () method is just a normal method call and does not have the ability to start a thread
The **start () method starts the thread and executes the code in run ()
The **currentthread () method returns the current thread object
/**
*
* Why can't I just create the thread class and call the Start method?
*
* The task code must be written in the Run method, and the Run method in the thread class does not implement any function and the code execution has no result
* So only inherit the thread class, override the Run method
*
**/
Multi-threaded Memory:
Open memory in the stack for the main thread
Multithreading does not follow the advanced out, each thread in the stack has memory, who first get the CPU, first execute
When the thread finishes executing its own task code, the thread disappears from the stack
The entire process ends only if all the threads are finished
The second way to create a thread:
To solve the critical resource problem, you need to use the second way to create a thread
1 creating subclasses that implement the Runnable interface
2 overriding the Run method of the Runnable interface
3 Create a subclass that implements the Runnable interface to the West Airlines
4 Creating an object of the thread class, that is, creating a thread
5 constructs a method for passing a subclass object of runnable Diet as a parameter to thread
* * The thread task is described, that is, object-oriented
* * Implementation of the separation of thread and thread objects, what tasks the thread performs is no longer important, as long as the subclass object that implements the Runnable interface can be passed as a parameter to the thread's construction method
* * You can inherit the parent class while implementing the interface
The life cycle of a thread:
/**
*
* Why the second way to create a thread can solve the problem of selling tickets?
*
* The first way to create a thread: thread and thread tasks are bound together, creating 4 threads creates 4 copies of a resource
* The second way to create threads: thread and thread tasks are separated, just create a task and let 4 threads execute separately
*
**/
Synchronized lock for code block: obj
Lock for sync function: this
Static synchronization functions:
Static functions do not exist when the object is in memory, but there is a byte-code file of its own class, which belongs to object of class type, so the lock of the static synchronization function is the bytecode file object of the class to which it belongs.
Avoid deadlocks:
Easy to happen in the locks of the nesting
Inter-thread communication:
The tasks between threads are different, but the data for the thread operation is the same
have shared data
Manipulate the code that shares data more than one sentence
Wait for wake-up mechanism:
Wait () notify () Notifyall ()
Must be used in sync, because locks are available in sync
Indicates that the thread holding the lock waits or is awakened
The waiting thread discards the lock
The **sleep () method causes the thread to enter a suspend (freeze), artificially
The **wait () method enters the wait state and puts the thread into the pool of threads
The **notify () method wakes any thread in the thread pool, allowing null wakeup, that is, there are no objects to wake up
**notifyall () method wakes all threads
/**
*
* Wait (), notify (), Notifyaall () Why is the definition in object?
*
* Wait (), notify (), Notifyaall () must be used in sync, because locks are available in sync
* Locks are arbitrary objects, methods that can be called by any object need to be defined in Obbject
*
**/
Multithreading--multithread