One, what is a thread? What is a process?
Process is the container of threads, first understand the process, the difference between learning thread
Description of the process:
Process is a computer program on a data set on a running activity, is the system of resource allocation and scheduling of the basic unit process consists of program, data and process control block three parts.
A program is a description of instructions, data, and its organization, and the process is the entity of the program. All resources related to the process are recorded in the Process control block PCB to indicate that the process has these resources or is using them.
The process is also the scheduling unit of the preemption processor, which has a complete virtual address space.
Description of the thread:
Threads are the smallest unit of program execution flow and are sometimes referred to as lightweight processes. A thread is an entity in a process and is the basic unit that is dispatched and dispatched independently by the system.
A standard thread consists of a thread ID, when a pro instruction pointer, a register collection, and a stack. It belongs to a process and shares the resources of the process with other threads in the process.
One thread can create and revoke another thread, and multiple threads in the same process can execute concurrently, and different threads within the same process share the same address space:
Second, the operating state
The status of the process:
1) Readiness Status (Ready)
The process has obtained the required resources in addition to the processor, waits for the processor resources to be allocated, and executes as long as the processor process is assigned. A ready process can divide queues by multiple priorities. For example, when a process enters a ready state due to time slices, it is queued to a high priority queue when the process is ready by I/O operations.
2) Operating status (Running)
The process consumes processor resources, and the number of processes in this state is less than or equal to the number of processors. When no other process can execute, such as when all processes are in a blocking state, the idle process of the system is usually performed automatically.
3) Blocking status (Blocked)
Because the process waits for a certain condition, such as an I/O operation or a process synchronization, the execution cannot continue until the condition is satisfied. The event does not run even if the processor resource is assigned to the process before it occurs.
The status of the thread:
1) New Thread state
Generates a thread object to generate a new one. When a thread is in the "New thread" state, it is simply an empty thread object that has not yet been assigned to system resources. Therefore, it can only be started or terminated. Any other operation throws an exception. For example, a thread can call the start and stop methods after it has called the new method and is in the state of a fresh thread before calling the Start method.
2) Operational state (Runnable)
The Start method produces the resources necessary to run the thread, dispatches the thread to execute it, and calls the thread's Run method. At this point the life state of the thread and the life state of the periodic thread are in a running state. This state is not called run state because the thread at this point does not always occupy the processor. In particular, for a PC with only one processor, there can only be one thread-taking processor in a functioning state at any given time. Note that if the thread is in the runnable state, it is also possible that it is not running, because there are also priority and scheduling issues.
3) Blocking State
When the following event occurs, the thread enters a non-running state.
The ①suspend () method is called;
The ②sleep () method is called;
③ threads use Wait () to wait for a condition variable;
The ④ thread is waiting for an I/O request.
4) Death State
When the Run method returns, or another thread calls the Stop method, the thread enters the dead state.
Three, common questions
1. What is a deadlock?
Specification definition for deadlocks: Each process in the collection waits for an event that can only be raised by another process in this collection, then the group process is deadlocked.
Deadlocks are two or more threads that are infinitely blocked, and threads wait for the required resources between them. This can happen when two threads try to acquire locks for other resources, and each thread is trapped in an infinite wait for other resource locks to be released, unless a user process is terminated.
A deadlock must have the following four necessary conditions.
1) Mutually exclusive condition: refers to the process of allocating resources to the exclusive use, that is, for a period of time a resource is occupied by only one process. If there are other processes requesting resources at this time, the requestor can wait until the resource-occupying process is freed.
2) Request and hold condition: means that the process has maintained at least one resource, but a new resource request has been made, and the resource has been occupied by another process, at which time the request process is blocked, but the other resources that you have obtained remain.
3) Non-deprivation conditions: Refers to the process has been obtained resources, before the end of use, can not be deprived, can only be released by themselves when the use is complete.
4) Loop wait condition: Refers to the deadlock, there must be a process-a circular chain of resources, that is, the process set {p0,p1,p2,,pn} P0 is waiting for a P1 to occupy the resources, P1 is waiting for the resources that P2 occupy, ..., Pn is waiting for the resources that have been occupied by P0.
Typical example: There is only one CD-ROM drive and one printer in the system, one process occupies the CD-ROM drive, the printer is applied, the other process occupies the printer, and the CD-ROM is applied. As a result, two processes are blocked and can never be lifted on their own.
2. What is a live lock?
A live lock is a special kind of starvation (starvation) (a phenomenon that a thread cannot do without the resources it needs for a long time).
The task or performer is not blocked, the data resource release time is uncertain, causing some transactions to wait for a long time, repeated attempts, failures, attempts, failures, and the chance of blocking.
A live lock should be a sequence of processes that wait for a certain impossible condition to be true in polling. The process is not blocked during a live lock, which can result in depletion of CPU resources.
Typical example: Two people meet in the narrow road, while avoiding in one direction, and then to another direction, so repeated.
3. What is thread synchronization?
Under multi-threaded routines, synchronization can control access to shared resources. If there is no synchronization, when a Java thread modifies a shared variable, another thread is using or updating the same variable, which can lead to incorrect results for the program.
Test examples:
Public classTest { Public Static voidMain (string[] args) {FinalOutputter output =NewOutputter (); NewThread () { Public voidrun () {Output.output2 ("Hello"); }; }.start (); NewThread () { Public voidrun () {Output.output2 ("World"); }; }.start (); } }classOutputter { Public synchronized voidoutput (String name) {//TODO in order to ensure that the output to name is not an atomic operation, here each character of name is output individually for(inti = 0; I < name.length (); i++) {System.out.print (Name.charat (i)); Try{Thread.Sleep (10); } Catch(interruptedexception e) {e.printstacktrace (); } } } Public voidoutput2 (String name) {//TODO in order to ensure that the output to name is not an atomic operation, here each character of name is output individually for(inti = 0; I < name.length (); i++) {System.out.print (Name.charat (i)); Try{Thread.Sleep (10); } Catch(interruptedexception e) {e.printstacktrace (); } } } }
3. What is the difference between Thread.Start () and Thread.run ()?
The Thread.Start () method (implemented by the local method, which needs to be called) initiates the thread to the ready state, which is dispatched by the JVM to execute the run () method when the CPU allocates time to the thread.
4. What is the difference between sleep (), suspend (), and wait ()?
Thread.Sleep () causes the current thread to be in a non-running (not Runnable) state at the specified time. The thread has been holding the object's monitor. For example, a thread is currently in a synchronous block or synchronous method, and other threads cannot enter the block or method. If another thread calls the interrupt () method, it wakes up the "sleeping" thread.
Note: Sleep () is a static method. This means that only the current thread is valid, and a common mistake is to call T.sleep (), where T is a different thread than the current thread. Even if T.sleep () is executed, the current thread goes to sleep, not the t thread. T.suspend () is an obsolete method that uses suspend () to cause a thread to go into a stagnant state, which will hold the object's monitor all the time, and suspend () can cause a deadlock problem.
Object.wait () makes the current thread out of a "non-operational" state, unlike sleep () where wait is the method of object instead of thread. after a thread executes the obj.wait () method, it frees the object lock it occupies, the a thread goes into a blocking state, and a does not have the power to get the Obj object so that the other thread can get the lock. Obj.notifyall () can wake up all the threads that are blocked by the Obj object and allow them to have the power to get the object, and Obj.notify () is the one thread that wakes up the block that is blocked by the Obj object.
Process, threading