Senior java se as much as a thread (a)

For me this opportunity to force food multi-threaded programmer used rarely, but when they especially like the interview to ask these things. Alas, a good memory as bad written, take the time to sum up, it should be a little deep impression.

 

The basic concept of the thread

Thread: is a control unit that performs the process of implementation of the road

  • A process in which at least one thread of execution in charge of the control program
  • A process if only one execution path, this is called a single-threaded program
  • When a process has multiple execution paths, this program has become a multi-threaded

A thread is a sequential flow of execution process. Multiple threads share the same piece of memory space and a set of system resources, the thread itself has a stack when the program is executed for. Thread switching the load is small, and therefore, the thread is also known as a light load process. A process can contain multiple threads.

In the JVM memory model, the thread opens up the stack, some senior call stack frame method for this thread stack space is a space frame, which is a process called a method, which in the stack to open up a space can also be considered a space of threads, after the end of the method, the thread is over, but the process still continues, will continue to the next method, continue to open threads.

Difference between threads and processes

A process has one or more threads. Thread more refined in the process, so that multithreaded programs concurrency high. The process has a separate memory unit in the implementation process, and multiple threads of shared memory, thereby greatly enhancing the efficiency of the program.

With the difference that the thread process during execution of each thread has an inlet a separate program running, an outlet and a program execution sequence order. But the thread is not able to execute independently, it must exist according to the application, providing multiple execution threads controlled by the application.

From a logical standpoint, meaning that a multi-threaded application program execution section may perform a plurality of simultaneously. But the operating system will not be seen as multiple threads to achieve multiple independent applications management and resource scheduling and allocation process.

Thread State

1. Create

Object statement creates a new thread in the new state, then it and other java objects, only allocated memory.

2. Wait

When a thread after a new, and before the start method is called, the thread is in a wait state.

3. Ready

When a thread object is created, other threads calling its start () method, the thread into the ready state. Thread in this state in the Java virtual machine can run the pool, waiting for the right to use the cpu.

4. Run state

Thread is in this state of occupied CPU, executes the program code. In a concurrent runtime environment, if the computer has only one CPU, so any time there is only one thread is in this state.

Threads have a chance only in a ready state to run state.

The blocked state

Blocking state means that thread for some reason to give up CPU, temporarily stop running. When a thread is blocked, Java Virtual Machine CPU will not be assigned to a thread, until the thread re-enter the ready state, it will have access to running.

Blocking state is divided into three types:

  1. Waiting for blocking: running thread execution wait () method, JVM will thread into the wait pool.
  2. Synchronous blocking: threads running at the time of acquiring the object synchronization lock, if the synchronization lock is occupied by another thread, the JVM thread will lock into the pool.
  3. Other blocking: execution threads running Sleep () method, or issues an I / O request, the JVM will thread to a blocked state. When the Sleep () timeout, or I / O processing is completed, the thread into the ready state again.

6. state of death

When the code thread executing the run () method, or encounter uncaught exception, it will exit run () method, then you enter the state of death, the thread end of the life cycle.

Thread of the life cycle

Thread life cycle is multithreaded very important knowledge for understanding the entire multi-threaded great help in using the system, the interview also like to ask the content.

Also the Internet to find relevant blog post, this blog can also write, speak life cycle thread.

https://www.cnblogs.com/linjiqin/p/3208494.html

 

Guess you like

Origin www.cnblogs.com/lqhhome/p/10849862.html