Java Concurrency Detailed study notes (a)

I. Introduction Thread

  Must first understand the process (Peocess) before speaking thread, the operating system now supports basic multi-tasking, for scenes: There are many programmers like coding side while listening to soft music. At this time the computer is doing parallel tasks, that is, there are multiple processes carried out at the same time. Program is a process having individual functions performed on the data set of a process, a process that is a short application process is the basic unit of resource allocation and scheduling system, and a process that contains at least one thread (Thread), a thread is in the process

Execution of a unit or entity to perform, with only a small amount of resources, has become a lightweight process thread, the thread is a processor (Java for JVM) scheduling and distribution of basic units.

  to sum up:

  • Process is the system were resources basic unit of scheduling and distribution
  • A thread is handler basic unit of scheduling and allocation
  • A process that contains at least one thread (containment relationship)
  • Operating system process creation and revocation of overhead is greater than the thread

  Understand the concept of threads we simply implement the following coding while listening to music:

public class TryConcurrency {

    public static void  coding() throws InterruptedException {
        while(true) {
            System.out.println("I am coding..");
            Thread.sleep(1000);
        }        
    }
    
    public static void listening() throws InterruptedException {    
        while(true) {
            System.out.println("I am listening music..");
            Thread.sleep(1000);
        }
    }
    
    public static void main(String[] args) {
        
        try {
            coding();
            listening();
        } catch (InterruptedException e) {
            
            e.printStackTrace ();
        }    
    }
}
View Code

  After the execution results:

  The result has been coding, and did not expect us to alternately perform, we know that the entrance Java se program is the main method, when the main method starts, JVM process will allocate a main thread to perform, the main thread is executed from top to bottom , it is performed to

coding () method is caught in an infinite loop can not be returned, it is listening () method programmed unreachable code that is not up to code. We want to perform on listening method also need to use a thread class Thread, so that the two methods "parallel" execution:

public static void main(String[] args) {
        new Thread() {
            @Override
            public void run() {
                try {
                    listening();
                } catch (InterruptedException e) {
                    e.printStackTrace ();
                }
            }
        }.start();
        
        try {
            coding();
        } catch (InterruptedException e) {
            e.printStackTrace ();
        }
        
    }
View Code

Let's look at the results:

We can see from the results met our expectations.

Second, the thread of life cycle

  A total of five thread life cycle, namely: new, runnable, running, blocked, terminated. That translated into Chinese: Create state, ready / executable state, running state, blocking state, end state.

Now let's talk about these in this state, the first thread Thread t t is to create a state, and then after the call start = new Thread () after () method ready state to wait for scheduling processor, the processor by polling, etc. Rear

You can enter the execution state, execution state of the following will thread into the blocked state:

  • After the initiative to call sleep, wait method of entering into the waitSet blocked
  • Entering a blocking IO operations, such as data network by reading and writing into the blocked state
  • Acquire a lock resources to be added to the lock of the blocking queue to enter the blocked state

The following thread cause it to enter terminated:

  • Thread running normal end, the end of the life cycle
  • Thread running error ended unexpectedly
  • JVM crash causes all threads end

Three, start the analysis method described

  We need to override the run method when creating Thread thread, and calls the start method to start a thread. Then the run method and the start method what relevance it?

  You can start reading the source code to know, first of all it is judged threadStatus this property is 0, 0 otherwise we can continue to throw an exception, then call start0 the JNI method, which is the native methods.

After the in-depth analysis can see that the thread enters execution state executable run in the JVM execution method. Play start reading the source code can have the following summary:

  1. Since threadStatus first determines whether the attribute is 0, then the new Thread apparent initial value is 0, then change. It can not be secondary start.
  2. The end of a thread of execution, that is, to the terminated state, can no longer invoke the start method.

 

 

  

 

Guess you like

Origin www.cnblogs.com/ring2/p/11075029.html