Multithreading Tutorial primary curriculum (a) - create multi-threaded approach

1-1: What is the difference between processes and threads?

Summary: The process is the set of all threads, each thread is a path of execution process.

1-2: What are the benefits of using multi-threaded

1. To improve the performance of programs, to prevent the full use of the performance of multi-core CPU 2. obstruction of

3. Use multi-threaded task can be achieved with a small Dahua purpose

2-1: Using inheritance Thread class implements multithreading

class the CreateThread the extends the Thread {
     // write multithreaded code to be executed in the run method 
    publicvoid run () {
         for (Inti = 0; I <10; I ++ ) { 
            System.out.println ( "I:" + I); 
        } 
    } 
} 
publicclass ThreadDemo { 
    publicstaticvoid main (String [] args) { 
        System.out.println ( "----- ----- multi-start thread is created" );
         // Create a thread 
        the CreateThread = CreateThread new new CreateThread ();
         // 2. open thread begins execution threads instead of calling attention to the run method, but start method 
        System.out.println ( "----- ----- start to create multi-threaded"); 
        CreateThread.start (); 
        System.out.println ( "Creating Finish ----- ----- multi-threaded" ); 
    } 
}

2-2: Implement Runnable interface, override the run method

class CreateRunnable the implements the Runnable { 
    @Override 
    publicvoid RUN () { 
        for (Inti = 0; I <10; I ++ ) { 
            System.out.println ( "I:" + I); 
        } 
    } 
} 
publicclass ThreadDemo2 { 
    publicstaticvoid main (String [] args) { 
        System.out.println ( "----- ----- multi-start thread is created" );
         // Create a thread 
        CreateRunnable = CreateThread new new CreateRunnable ();
         // 2. open thread begins execution threads instead of calling attention to the run method, but start method
        System.out.println ( "----- multi-threaded create a startup -----" ); 
        the Thread the Thread = new new the Thread (CreateThread); 
        Thread.start (); 
        System.out.println ( "---- - create multi-threaded end ----- " ); 
    } 
}

2-3: Use an anonymous inner class way to achieve multi-threaded

System.out.println ( "----- multi-start thread is created -----" ); 
         the Thread Thread = new new the Thread ( new new the Runnable () {
             public  void RUN () {
                 for ( int I = 0; I < 10; I ++ ) { 
                    System.out.println ( "I:" + I); 
                } 
            } 
        }); 
         Thread.start (); 
         System.out.println ( "----- ---- end Create multithreaded - ");

3-1: Using inheritance Thread class or Runnable interface that implement good?

That implement Runnable interface to achieve good reasons implements the interface can continue inheritance, the class can not be inherited.

3-2: Start the thread is still run using the start method is called method?

Start method is called, if the call to run just call similar to an ordinary method, when the thread runs in its essence is not created. Just run the equivalent of calling the main thread calls a generic method

4-1: Simple API common thread

Common thread api method 
start () start a thread 
currentThread () Gets the current thread object 
getID () Gets the current thread ID the Thread - Number This number from 0 
getName () Gets the current thread name 
SLEEP ( Long Mill) sleeping thread 
Stop () to stop a thread 
common thread constructor 
thread () Allocates a new thread object 
thread (String name) Allocates a new thread object with the specified name as its name. 
Thread (Runable r) assign a new Thread object 
Thread (Runable r, String name) is assigned a new Thread object

 

5.1: multi-threaded operating status

Thread from creation, always run to the end in one of the following five states: New state, ready state, running state, blocking state and the state of death.

New Status

When creating a new thread with the operator, for example, new Thread (r), the thread has not started to run, in this case a new thread state. When a thread is in the nascent state, the program code that has not yet started running thread

Ready

A new thread is created does not start automatically, to execute the thread, the thread must call the start () method. When calling thread object start () method to start a thread that is, create a system resource threads running start () method, and schedule threads run run () method. After the start () method returns, the thread is in the ready state. Thread a state of readiness does not necessarily run immediately run () method, the thread must also compete with other thread CPU time, CPU time before they can obtain only running thread. Because a single CPU in a computer system, it is impossible to run multiple threads simultaneously, one time only one thread is running. Therefore, at this time there may be multiple threads in the ready state. The system of multiple threads in the ready state is run by the Java thread scheduler (thread scheduler) to schedule.

Operating status

When the thread gets CPU time, before it went into operation, really started run () method.

Blocking state

Thread during operation, may enter the blocked state due to various reasons: the thread to sleep by calling the method sleep; Thread a call blocked on I / O operation, i.e. the operation is completed before entering the output operation does not return to its the caller; thread trying to get a lock, but the lock is being held by another thread; thread is waiting for a trigger condition;

Death state

There are two reasons why the thread death: run method exits normally and died of natural causes, an uncaught exception terminates the thread run method sudden death. In order to determine whether the current thread alive (that is, either can be run either be blocked), use isAlive method. If you are running or blocked, it returns true; if the thread is still new and not run the state, or the death of a thread, false is returned.

Guess you like

Origin www.cnblogs.com/itcastwzp/p/10971454.html