Two ways to achieve thread

First, the Thread class inheritance

  Thread class is a class Java.lang package, from the class to instantiate the object represents a thread, start a new thread needs to establish Thread instance

  Thread configured as follows:

    public Thread (): create a new thread object.

    public Thread (String ThreadName): create a name for ThreadName thread object.

  Inherit the Thread class to create a new thread syntax is as follows:

public class ThreadTest extends Thread{
}

  Complete threaded code run on the actual function of the class () method, when a class inherits from Thread class, the class can override the run () method, the code that implements the function is written to the thread run () method and then call simultaneously thread class start (0 thread of execution method, which is calling run () method.

  Thread object needs to perform a task, the task refers to the worker threads at startup, the work function code is written in the run () method. 

  run () method syntax:   

public void run(){
 }

 

 note:

    If the start () method is called a thread has been started, the system will throw an exception IllegalThreadException

 

  

  When executing a program thread, but will automatically generate a thread, the main method it is running on this thread when other threads does not start, the program is single-threaded programs  

  Start threaded code as follows: 

Public static void main(System[] args){
    new ThreadTest().start();
}

  

  Example 1:

    ThreadTest class created in the project, the class inheritance Thread class method creates a thread 

package inheritance Thread class; 

public  class ThreadTest the extends Thread {     // the specified class inherits class Thread 
    Private  int COUNT = 10 ; 

    public  void run () {                // override run () method of 
        the while ( to true ) { 
            System.out.println (COUNT + "");  // print count variable 
            iF (--count == 0) {          // use count variable is decremented when the decrement to 0, the loop exits 
                return ; 
            } 

        } 
    } 

    public  static  void main(String[] args) {
        new ThreadTest().start();
    }
}

   operation result:

  The above-described example, inherited Thread class, and then covering the run () method in the class usually used in the form of an endless loop, is too thread go on indefinitely, so to specify a condition for the loop out, such as the use of variables in this example, count decremented to 0 as a condition out of the loop.

  In the main method, the thread execution needs to call Thread class start () method, start () method is called run-covered () method, if you can not afford to use start () method, the thread never starts in the main method no calls start () method before, thread instance of an object rather than just a thread.

Second, implement Runnable

  A GUI program Jframe extension classes can no longer inherit the Thread class, because Java does not support multiple inheritance. If you want to use the current class multi-threaded, it can Runnable interface.

  Runnable interface to achieve the following syntax:

public class Thread extends Object implements Runnable

  Description:

    Substantially Thread class implements Runnable interface, wherein the run () method is embodied on the Runnable interface run () method.

  Program implement Runnable create a Thread object and Runnable Thread object associated with the object.

  Thread class constructor has the following two methods:

    public Thread(Runnable target)

    public Thread(Runnable target,String name)

  Examples of these two parameters Runnable present construction methods, using the above method can be configured Runnable Thread instance associated with the instance.

  Step Runnable interface using the following starting a new thread;

    (1) establish a Runnable object

    (2) Examples of parameters to create Thread Runnable object's constructor

    (3) call to start () method to start a thread

  Through the Runnable interface to create threads, you first need to write a class that implements the interface Runnable, then instantiate objects of that class, thus establishing Runnable object; Next Thread instance is created using the appropriate constructor; use the last instance of the Thread class calls the start () method to start a thread. Figure 1 shows that the implementation process Runnable interface to create threads

  figure 1

Example 2

  Implement Runnable way to create a thread

package Example2;
/**
 * 实现Runnable接口方式创建线程
 * @author yangf
 *
 */
public class MyRunnable implements Runnable{//实现Runnable接口

    @Override
    public void run() {
        // TODO Auto-generated method stub
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName()+":"+i);
        }
    }
    public static void main(String[] args) {
        //1. Create a thread object 
        the Runnable myRunnable = new new MyRunnable ();
         //
         the Thread T = new new the Thread (myRunnable, "the MyThread" ); 
        t.start (); // start the thread 
    } 

}

  The result: 0-99

 

  note:

    Start a new thread, not directly call the Thread subclass object's run () method, and call the start Thread subclass () method, start Thread class () method creates a new thread that runs Thread subclass run () method.

Third, the summary

 

Call the thread object start () method and call the run () method difference:

  run (): Only the main thread a path of execution

  start (): a plurality of execution paths, the main thread and the child thread are alternately performed in parallel

 

Guess you like

Origin www.cnblogs.com/YangYouHan/p/11011092.html