The purpose of multithreading
When it comes to multithreading, there is only one directory, and better utilization of CPU resources, we allow the CPU to handle multiple tasks simultaneously, shortening computation and processing time.
Before implementing multithreading, first understand some of the concepts under Multithreading:
Multithreading: Running more than one thread in a process
Parallel: Multiple CPU instances or multiple computers running a processing logic at the same time, in which case it is true that the same value is on the line.
Concurrency: Through the CPU scheduling algorithm, so that the user seems to be in parallel, but in fact, the CPU is not really at the same time, the following quoted netizens a picture:
Thread Safety: Thread safety is generally a variable or a piece of code, that is, in the case of concurrency, the code is multithreaded use, but the scheduling order of the thread does not apply the final result, we write the program only need to consider the system's memory and CPU is sufficient, in turn, Non-thread-safe even if the thread's line of precedence may cause a different result
Synchronization: Synchronization in Java refers to the use of human control program value lines, to ensure that common resources to multi-threaded access is thread-safe, general synchronization has a variety of implementations such as synchronized keyword and CAS algorithm, etc.
The state of the thread
The thread starts from the beginning to the result by following several states:
NEW: Creates a new thread that has not yet started running
RUNNABLE: Threads run in a Java virtual machine, but wait for operating system resources, such as CPU resources
BLOCKED: Thread is blocked while waiting for the monitor lock to be released into the sync method
Waiting: Thread waits, the state is the current thread waits for another thread value line a specific program
Timed_waiting: And waiting type, but the wait will specify a specific time
TERMINATED: Process Run complete
The above status can refer to the Java source code to describe the status of the process: thread.state
Transition between process states
Lock
A lock in Java is a monitor that provides the necessary support. A logical lock is a piece of data in the head of an object's memory heap, each object in the JVM has a lock (mutex) that any program can use to coordinate access to the object, and if any program wants to access the object instance, it must obtain a lock on the object (with a locale in the lock). Other threads attempting to access the object must wait until the instance that acquires the lock releases the lock (the token that alters the lock)
Locks are used to protect code fragments, and only one thread can execute the protected code at any time, and if a method uses the Synchronized keyword declaration, the object's lock will protect the entire method
Monitor
A monitor can be thought of as a special room with some data and code, but only one person (consumer) can use the room at a time, and if you want to enter the room, you must go to the lobby (Entry Set) to queue, the scheduler will use a certain standard such as FIFO, Select a consumer (thread) from the lobby to enter a special room, if the thread is suspended for some reason, it will be scheduled by the scheduler waiting Room (wait Set), and after a period of time to reassign to a special room.
Simply put: A monitor is used to monitor a thread into a special room, and he ensures that only one thread at a time can access data and code in a particular room.
Wait/notify must exist in synchronized, and these three keywords are for the same monitor
Java Multithreading Implementation (i)