Multithreading --Thread class

Process (Process): "program is being executed" to enter the memory to run the program becomes a process. A process that will produce multiple threads.

Multithreading (Multithread): a few executable process exist. Function is executed in the order of a single-threaded, multi-threaded multi-segment code is run while alternately. A core CPU, at a time only execute one thread, CPU quickly switch between multiple threads.

Thread scheduling: time-sharing and preemption. Time-sharing scheduling is pre-allocated for each thread. Preemptive scheduling is a priority.

Multi-threaded implementation - inheritance Thread class

public  class the MyThread the extends Thread {        // inherits from Thread class 

    public the MyThread (String name) {           // constructor call parent class 
        Super (name); 
    } 
    public  void run () {                      // override the run method, the thread to be executed tag is placed within the run method 
        for ( int I = 0 ; I < 20 is ; I ++ ) { 
            the System. OUT .println (getName () + " :! is being executed " + . I); // Thread.currentThread () getName ( ); Gets the name of the current thread object 
        } 
    } 


}
public  class the Test {
 public  static  void main (String [] args) { 
    the MyThread MT = new new the MyThread ( " my thread " ); 
    mt.start (); 
    for ( int I = 0 ; I < 20 is ; I ++ ) { 
        the System. OUT .println ( " main function thread! " + I); 
    } 
    . the System OUT .println ( " main function performed ended " ); 
} 
}

From the operating results, run the order to seize the two threads by the thread to CPU resources may be.

 Thread.currentThread () Gets the current thread object

 Thread.currentThread().getName();获取当前线程对象的名称

Guess you like

Origin www.cnblogs.com/zhai1997/p/11369562.html