high concurrency java programming explain - Chapter Multithreading Basics

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/qq_40646143/article/details/90691912

Introduction Thread

  • Now almost 100% of the operating systems support multi-tasking on the computer for each task is a process (Process), in every internal process must have at least one thread (Thread) is in operation, sometimes also called thread as a lightweight process.
  • A thread is a path of program execution, each thread has its own local variable table, program counter (instruction pointer is being performed), and their life cycle, modern operating systems generally run more than one thread, when a start java virtual machine, the operating system will start to create a new process (JVM process), JVM process will be derived or create many threads.

 

Detailed thread life cycle

 

Introduced to Thread

  1. NEW
  2. RUNNABLE
  3. RUNNING
  4. BLOCKED
  5. TERMINATED

 

1.1 thread NEW state

       When we create a Thread object with the keyword new, this time it is not being executed, because there is no call start method to start the thread, the thread state NEW state, to be exact, it is only Thread object's state, because there is no before start, the thread does not exist, there is no difference with you to create a normal java object with the keyword new.

      NEW is RUNNABLE state by the start method.

 

2.1 thread RUNNABLE state

       RUNNABLE thread object into the state must start method is called, then the time is really to create a thread in the JVM process, once started threads can be implemented immediately? The answer is no, or not running threads and processes to be the same listen up and CPU scheduling, so we put this intermediate state called executable state (RUNNABLE), which means that it has qualified to perform, but did not really execute them but waiting for CPU scheduling.

      Due to the Running state, it does not directly entered the BLOCKED state and TERMINATED state, even calling wait, sleep or IO operations other block in the execution of logical thread, etc., it must first get scheduled for execution of the CPU can strictly be say, RUNNABLE unexpected termination or thread can only enter the RUNNING state.

 

RUNNING state 3.1 thread

       Once selected thread queue by polling or CPU can perform tasks from other means, then the time it can really implement their own logic code, you need to note it is that a state is RUNNING thread is in fact RUNNABLE, but in turn, It is not true.

In this state, the state of the thread may occur following state transitions:

  • TERMINATER directly into the state, such as calling JDK has been deprecated stop method, or determine if a logical identification
  • Entered the BLOCKED state, for example, called sleep, or wait method to join in the waitSet
  • Conduct a blocking IO operations, such as read and write because of network data entered the BLOCKED state
  • Acquire a lock resources to be added to the lock of the blocking queue to enter the BLOCKED state
  • Since the CPU scheduler performs polling so that the thread to give proceeds state RUNNABLE
  • Thread the initiative to call yield method, renounce the use of executive power CPU, enter RUNNABLE state

 

BLOCKED state 4.1 thread

  • TERMINATER directly into the state, such as calling JDK has been deprecated stop method used to enter or accidental death
  • Thread end blocking operation, such as reading the desired data bytes into the state RUNNABLE
  • Thread finishes dormant specified time, into the state RUNNABLE
  • Wait in the thread wakes up another thread notify / notifyall, into the state RUNNABLE
  • Thread gets to a certain lock resources into the state RUNNABLE
  • Thread is interrupted in the course of obstruction, such as other thread calls interrrupt method, enter RUNABLE state

 

TERMINATED state 5.1 thread

       TERMINATED is the final state of the thread, the thread in this state will not switch to any other state, the thread enters TERMINATED state, meaning that the entire life cycle of the thread is over, these will make the following thread enters RUNMINATED state

  • Thread running normal end, the end of the life cycle
  • Thread running error ended unexpectedly
  • JVM Crash, causing all have to thread the end

 

to sum up

  • There are two ways to create threads, one is to create a Thread, one is to achieve Runnable interface, this argument imprudent. Accurately speaking, only one way to create a thread that is constructed Thread class, and achieve the thread execution unit there are two ways, first is to rewrite Thread run method, the second method is to achieve run Runnable interface, Runnable configuration example of a user and the parameters of Thread.

 

     Thread Thread run method difference method and class run Runnable interface?

  • Thread class is run method can not be shared, i.e. method A thread can not run B as its own execution thread Runnable interface unit is very easy to achieve this, a different example of the configuration of the same Runnable Thread instance.

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_40646143/article/details/90691912