Day62: java- multithreading

 You need to know:

The concept: procedures, processes, threads

Creating and using multiple threads

Thread of the life cycle

Thread synchronization

Communication thread

 

The concept: procedures, processes, threads

Program - Process - Thread
Thread: is an execution path of an internal program (if the process compared to the river, then the thread is equivalent to a branch of the river)
the CPU core number: cpu on behalf of a few core can handle a number of tasks instantaneous time
master frequency: switching speed between tasks
more auditing, clocked the sooner the better


Creating and using multiple threads

Multithreading: a process (a program run), can differentiate into multiple threads execute in parallel (multiple subroutines) when you need multi-threading? 1, the program needs to perform two or more tasks simultaneously 2, you need a program running in the background of some multi-threaded creation and start-up is achieved by java.lang.Threat the Thread class characteristics: RUN (): each thread through a specific run thread object () method (its main body, called threads, write code logic you want to run in this method) to complete the operation 
start (): by the start () method (start thread) the thread object to call this thread

creation thread:
method one: multithreading thread class inheritance to achieve

CC.java
Package Jicheng; 

Import the java.lang *. ; 

public  class the CC the extends the Thread { 

    @Override 
    public  void RUN () { 
        System.out.println ( "written here is to run multithreaded code" );
         for ( int I =. 1; I <=. 8; I ++ ) 
            System.out.println (I); 
    } 
}

B.java
Package Jicheng; 

Import the java.lang *. ; 

public  class B {
     public  static  void main (String [] args) {
         // new new instance of the parent class when used to receive can (polymorphic objects) 
        the Thread = T0 new new the CC (); 
        t0.start (); // start the thread, run run method
 //         other code to run code execution main method after a good start () method of parallel 
        System.out.println ( "***** " );
         // find the results of each run is not the same
         // output and output mixed run method, and the order is not necessarily, but keep their output order
         // this is a multi-threaded asynchronous, synchronous: in strict accordance with the code from the to the next execution
         // asynchronous: open thread after the run method of operation and the main program code is executed in parallel, and has no relations
    }
}

 


Method Two: Implement Runnable interface to achieve multi-threaded

 

Guess you like

Origin www.cnblogs.com/OFSHK/p/12622333.html