Http://www.mamicode.com/info-detail-517008.html
1. Three states of the process
- Ready state: The process obtains all resources except the CPU and executes as long as the processor is acquired
- Execution Status: Process acquisition processor, processing on-board execution
- Blocking state: The process waits for an event to occur (such as waiting for I/O to complete), discards the processor and is blocked
2, thread of the concept, the basic state of the thread
- New
New creates a thread object that allocates memory
- Wait
After new, start ()
- Ready
After star (), the CPU is in a running thread pool, waiting for the CPU to dispatch
- Run
Get CPU execution, only one thread is running at any moment, only the thread in the ready state has a chance to enter the running state
- Blocking
- Wait for blocking: The running thread calls the Wait () method, the JVM puts the thread into the waiting pool, and only receives the Notify () or notifyall () message before it enters the ready state (can be run)
- Synchronous blocking: When a running thread acquires a synchronization lock on an object, the synchronization lock is occupied by another thread, and the JVM puts the thread into the lock pool
- Other blocking: The running thread calls the sleep () method, or makes an I/O request, and the thread re-enters the ready state when sleep () times out or I/O completes
- Death
Execute run ()
3. Three ways to create threads
Reference: [http://blog.csdn.net/longshengguoji/article/details/41126119]
- Inherit the thread class, overriding run
- Implement the Runable interface, implement run, implement an instance of the class as a parameter of the thread class passed in
- Through the callable interface and the future interface
- Create a callable implementation class to implement the Call method
- Create an object of the implementation class and wrap the object with the Futuretask class
- Futuretask object creates thread as parameter of thread
- Call Futuretask's Get () to get the return value after the child thread executes
Contrast:
When using the runnable, callable interface to transcend multiple threads, the advantages are:
The thread class simply implements the Runnable interface or callable interface and can inherit other classes.
In this way, multiple threads can share the same target object, so it is very suitable for multiple identical threads to handle the same resource, so that the CPU, code and data can be separated to form a clear model, which is a good embodiment of object-oriented thinking.
Disadvantages are:
Programming is slightly more complex, and you must use the Thread.CurrentThread () method if you want to access the current thread.
The advantage of creating multiple threads with the inherited thread class is:
Writing is simple and if you need access to the current thread, you do not need to use the Thread.CurrentThread () method to get the current thread directly using this.
Disadvantages are:
The thread class has inherited the thread class, so it is no longer possible to inherit other parent classes
4. Thread Blocking
- Sleep ()
A thread is blocked for a specified time, and the specified time is re-entered in the Ready state
- Wait () and notify ()
- Wait (XXX) when the corresponding notify () is called or exceeds the specified time, the thread will go into the ready state
- Wait () will only enter the ready state if the corresponding notify () is called
- Suspend () and resume ()
Suspend () causes the thread to enter a blocking state and is not automatically restored. Only when the corresponding resume () is invoked will it enter the ready state
- Yield ()
Causes the thread to discard the currently acquired CPU time, but does not enter the block, the thread is in the ready state, ready to get CPU execution
- Sleep () and wait differences
- Sleep (): in thread; the thread pauses execution for a specified time, and then automatically enters the operational state and does not release the object lock
- Wait (): in object; thread frees the object lock, enters the secondary object's waiting lock pool, the thread will enter the operational state only for the secondary object notify or Notifyall
Java Knowledge Review-multi-threading