Java basics-003, java-003
10. Processes and threads
A process is the executor application, and a thread is an execution sequence inside the process. A process can have multiple threads. A thread is also called a lightweight process.
Three ways to create a thread:
I> inherit the Thread class
II> implement the Runnable interface
III> applications can use the Executor framework to create thread pools.
Runnable interface implementation is more popular because it does not need to inherit the Thread class. when a class object has been inherited in the design, this requires multi-inheritance (but Java does not support multi-inheritance) and can only implement interfaces. at the same time, the thread pool is also very efficient and easy to implement and use.
Thread status:
I> new: a new object is created.
II> runnable: After the thread creates an object, other threads (such as the main thread) Call the start () method of the object, the thread in this status is located in the runable thread pool, waiting to be selected by the thread scheduling to obtain the CPU usage right.
III> running: A running thread obtains the CPU time slice and executes the code.
IV> block: the blocking state means that the thread gives up the CPU usage right for some reason, that is, it gives up the CPU time slice and stops running temporarily. it is not until the thread enters the running state that it has the opportunity to obtain the CPU time slice again and go to the running state.
Blocking is divided into three types:
A> waiting for blocking: The Running thread executes the wait () method, and JVM puts the thread into the waiting queue.
B> synchronization blocking: When a running thread obtains the synchronization lock of an object, if the synchronization lock is occupied by another thread, the JVM puts the thread in the lock pool.
C> Other blocking: The Running Thread executes the Thread. sleep (long MS) or t. the join () method, or when an I/O request is sent, JVM sets the thread to blocking. when the sleep () State times out, the join () waits for the thread to terminate or time out or the I/O processing is completed, and the thread is re-transferred to the runable state.
V> dead: the execution of the run (), main () method ends, or the run () method exits due to an exception, the dead thread cannot be re-generated.
11. Synchronization Methods and code blocks
The synchronization method uses this or the current class object as the lock by default.
You can select the locking method for the synchronization code block, which is more fine-grained than the synchronization method. We can choose to only synchronize part of the code that will cause synchronization problems, rather than the entire method.
12. MonitorThe monitor and lock are used together in the Java Virtual Machine. the monitor monitors a synchronization code block to ensure that only one thread executes the synchronization code block at a time. each monitor is associated with an object reference. the thread cannot execute the synchronization code before obtaining the lock.
13. deadlockTwo or more processes have a deadlock when they wait for the execution to continue. As a result, both processes are in the infinite wait state.
When multiple threads are used, a very simple way to avoid deadlocks is to specify the order in which the deadlocks are obtained and to force the locks to be obtained in the specified order. therefore, if all threads are locked and released in the same order, no deadlock will occur.