Create multi-threaded way: implement Runnable

1. Create a class that implements the interface Runnable

2. To achieve the abstract class to implement the method of Runnable: run ()

3. Create an object implementation class

4. This object is passed as an argument to the constructor of the Thread class, create an object class Thread

Start method is called by the object class Thead

Comparison of two ways to create threads:

Development: preferred way to achieve Runnable interface

  Limitations of single inheritance embodiment not implemented class 1

  2. To achieve a more appropriate way to handle multiple threads have shared data situation

联系:public  class Thread implements Runnable

Similarities: Both approaches need to override run (), a logical statement will be executed in the thread run () in

 

Thread of the life cycle

  New: When the object chant declared a Thread class or sub-class is created, the nascent thread object in the new state

  Ready: the new thread is a state is start (), the thread will enter the queue waiting for CPU time slice, at this time it has with the conditions of operation, but no CPU resources

  Run: When ready thread is scheduled and access to CPU resources, it went into operation, run () method defines the operations and functions of the thread

  Blocking: Under certain exceptional circumstances, be suspended or artificially perform input and output operation, so that the CPU and temporarily suspend their execution, enter the blocked state

  Death: thread to complete all of its work in advance or thread is forcibly suspended or abnormal lead to an end

Ready to run: Get the executive power CPU, the reverse is the loss of executive power or yield ()

Blocking: sleep () join () waits for synchronization lock wait () 

 

Guess you like

Origin www.cnblogs.com/ergePython/p/11885691.html