Write in front:
Knowledge points such as multithreading concurrency involve a wide range and a large degree of extension. Some basic knowledge about threads are organized by the theory and source code method. Other more and deeper knowledge will be learned and organized one by one later, learning the basics first, and then extending.
table of Contents
Three, thread life cycle and state transition
4. Analysis of common operation methods
Six, common concurrent containers
One, threads and processes
A process refers to an application program running in memory. Each process has its own independent piece of memory space. It is an independent unit for system resource allocation and scheduling and the smallest unit of program operation.
A thread refers to an execution flow in a process. It has no memory space of its own and is the smallest unit of cpu operation and dispatch.
In Java, at least two threads are started each time a program runs: one is the main thread and the other is the garbage collection thread. Because every time a class is executed using the java command, a JVM is actually started, and each JVM actually starts a process in the operating system.
Second, thread implementation
Inherit Thread
Since java is single inheritance, this kind of implementation is basically unnecessary
Implement Runnable
Interface, rewrite run()
实现Callable&Future
to be continued
Three, thread life cycle and state transition
New
new [coming to the stadium]
Runnable
runnable 【Enter the run-up area】
After the thread object is created, other threads call the start method of the thread, enter the thread pool and wait for the scheduling to be selected, and obtain the right to use the cpu. The sleep() method of the current thread ends, and the join() of other threads ends, waiting for the user's input to complete, and a thread gets the object lock, and these threads will also enter the runnable state. The current thread time slice is used up, call the yield() method of the current thread, and the current thread enters the runnable state. After the threads in the lock pool get the object lock, they enter the runnable state.
run
running 【Starting】
Obtain the cpu time slice (timeslice) in the runnable state, and execute the program.
block
blocked [Stop and do something]
The thread gave up the right to use the cpu for some reason, gave up the cpu timeslice, and halted the operation. Until the thread re-enters runnable.
The so-called blocking state is that the running thread has not finished running, and the CPU is temporarily surrendered. At this time, other threads in the ready state can obtain CPU time and enter the running state.
There are three types of blocked:
waiting to block the execution of the wait() method, entering the waiting queue (waitting queue) to
synchronize the blocking running thread when acquiring the synchronization lock of the object, if the synchronization lock is occupied by another thread, the JVM will put it in the lock In the lock pool, it will be runnable after the lock is obtained.
Other blocking Thread.sleep(long ms) sleeps for five minutes, wakes up and then enters the thread pool
t.join() ran and was suddenly jumped into the queue, When you have to stop
making an IO request, the JVM will put the thread in a blocked state. You are running, you go to check the file, so you have to stop.
When the sleep() state times out, wakes up,
join() waits for the thread to terminate or time out, the person who jumped in the queue is gone or hangs up,
or the I/O processing is complete. Over
The thread returns to a runnable state.
wait
waiting 【Wait, someone called to run again】
Threads in this state will not be allocated CPU execution time, they have to wait to be explicitly awakened, otherwise they will wait indefinitely.
Timeout waiting
time waiting [Wait, after the waiting time, you won’t wait]
Threads in this state will not be allocated CPU execution time, but there is no need to wait indefinitely for being awakened by other threads. They will automatically wake up after a certain time.
termination
terminated [After running or leaving by accident]
The execution of run(), main() ends, or the run() method exits due to an exception, ends the life cycle of the thread, and the death cannot be revived.
4. Analysis of common operation methods
Thread.sleep(long millis)
This method must be called by the current thread. The current thread is blocked, but the object lock is not released. After millis, the thread automatically wakes up and enters a runnable state. Role: The best way to give other threads a chance to execute.
Thread.yield()
This method must be called by the current thread, and the current thread abandons the cpu time slice obtained, and changes from the running state to the runnable state, allowing the OS to select the thread again. Function: Let threads of the same priority execute in turn, but there is no guarantee that they will execute in turn. In practice, there is no guarantee that yield() will achieve the concession purpose, because the concession thread may be selected again by the thread scheduler. Thread.yield() will not cause blocking.
t.join()/t.join(long millis)
The join method of other thread 1 is called in the current thread. The current thread is blocked, but the object lock is not released. Until the execution of thread 1 is completed or the millis time is up, the current thread enters the runnable state.
obj.wait()
The current thread calls the wait() method of the object, and the current thread releases the object lock and enters the waiting queue. Rely on notify()/notifyAll() to wake up or wait(long timeout) timeout to automatically wake up.
obj.notify()
To wake up a single thread waiting on this object monitor, the choice is arbitrary. notifyAll() wakes up all threads waiting on this object monitor.
Five, thread pool
ThreadPoolExecutor extends Executor
Six, common concurrent containers
ConcurrentHashMap
to be continued
Other concurrent containers
to be continued
queue
Synchronization queue
When the current thread wants to call the synchronization method of object A, it finds that the lock of object A is occupied by another thread. At this time, the current thread enters the synchronization queue. In short, the synchronization queue contains threads that want to compete for object locks. Synchronous queue is a concept that exists only in a synchronous environment, and an object corresponds to a synchronous queue.
Lock pool queue
The lock pool contains threads that want to compete for the object lock. When a thread 1 is awakened by another thread 2, thread 1 enters the lock pool state to compete for the object lock.
The lock pool is a concept that exists only in a synchronized environment, and an object corresponds to a lock pool.
Waiting queue
to be continued
Seven, thread safety
Definition: There is a conflict between preemption of CPU usage rights and resource sharing