One, multi-threaded.
1. Process: An executing program is called a process.
The execution of each process has an order of execution, which is a path to execution, or a control unit.
2, Thread: is an independent control unit in the above process, the thread is controlling the execution of the process.
A process has at least one thread.
Java Virtual machine starts with a process Java.exe that at least one process is responsible for the execution of the Java program,
The code running by this process exists in the main method.
And that thread is called the main thread.
Extension: In a more detailed description of the JVM, the JVM starts with more than one thread in the main function, and a Java garbage collection mechanism thread.
Second, how to define a multi-threaded in the custom code?
In Java, the threads are described and encapsulated into the thread class,
There are two ways to create a thread:
The first way to create a thread: is to inherit to the thread class
Step: 1, define a class, inherit the thread class.
2. The Run method in the replication class.
In order to define the code that the thread will execute.
3. New object, create thread.
4. Call its Start method,
This method has two functions: 1), starting thread 2), calling the Run method.
The program in the main function is at the same time as the program in run.
Actual results: The results of each execution are found to be different, because multiple threads are acquiring the CPU's resources, and the CPUs are executing to that thread, which thread will start running.
And every moment the CPU is running only one process (thread), the CPU is doing a high-speed switchover. This switch has
So: Randomness is a feature of multithreading.
The reason for overriding the Run method in a subroutine: Because the thread class is to describe a thread, the class has a function: to store the code to execute.
This function is just the run () method. That is, the code in the Run () method is the code that the thread executes (similar to main).
Note: Multi-threaded execution only occurs after the multithreaded Open statement is executed (the start statement only turns on multithreading).
The second way to create a thread is to declare the implementation of a runnable interface.
Step: 1, define a class, it implements the Runnable interface,
Class Test implements Runnable
2. Make a copy of the Run method that implements runable in this class.
Determine if code should execute in thread: public void Run () {}
3 A new object of the class T.
Test T = new Test ();
4, new A thread method, and the object T as a construction argument to pass in (that is, the specified thread to execute the Run method).
Thread th = new Thread (t);
5. Call the Start method of the Thread object,
Th.start ();
The difference between the way of inheritance and implementation:
Implementation: Avoid the limitations of single inheritance, when defining multithreading, it is recommended to use the implementation method.
Its thread code is stored in the Run method of the interface subclass object. (You can implement multiple threads to execute the same code as long as the object passed into the thread is the same)
Inheritance: Its thread code is stored in the object run method of the thread subclass, and a thread can execute only the code in its own run.
Third, the method in the thread
1, Thread, defines a name of a private string type variable, the new Thread object can be passed the string name,
(Note: duplicate constructor super () when creating subclass objects)
Use the GetName () method to get this variable.
In fact, the thread has a default name: The thread-number, which starts at 0.
Static thread CurrentThread (): Gets the object of the current thread.
GetName (): Gets the name of the thread. Instead, set the name of the thread: SetName (), or the constructor.
Iv. security issues in multi-threading
Cause of the problem: because multiple threads operate on the same shared data (implementing a method operation on the same object, or inheriting a method operation of a static data), where a thread executes only part of it, does not finish,
A shared data error occurs when another thread comes in to participate in execution.
Workaround: When multiple threads operate on the same shared data, only one thread is allowed to manipulate the data, and other threads cannot participate in the execution of the process.
Java provides a professional solution for threading security issues:
1. Synchronous code block:
Synchronized (object)
{
The code that needs to be synchronized (here is the data code);
}
The object here is any object, like a lock, only the thread that holds the lock can execute the data code synchronously;
Without a lock process, the data code cannot be executed even if the CPU is executed. (Example: The toilet on the train--the old example)
Benefit: You must ensure that only one thread in the synchronization is running.
Disadvantage: Reduce the program efficiency.
2. Non-static synchronization function:
is to place the Synchronized keyword in the modifier position of the function.
For example:synchronized Void Run () {} (Note: The Run method is not normally synchronized, just for example.) )
The "lock" (object) of a synchronization function is the object that is called when this method is invoked, that is: the equivalent of this
3. Static synchronization function:
If the above-mentioned synchronization function is statically modified,
Its static "lock" is the bytecode object of the class that called the method, namely: Class name. class "Type class"
(Because the static method is in memory, there are no objects of that class, but there are bytecode objects of that class, using that bytecode object as a synchronous lock)
The premise of synchronization: 1, more than two threads are executing the same code,
2. They use the same lock.
Five, deadlock
Cause: Synchronization is nested in sync, and the sync lock is not the same. (prevent this from happening).
Multithreading (thread,runnable)