Three ways

1. Create a Thread class by inheriting the Thread class and start a new Thread using the start method

Usage:

  1. Inherits the Thread class and overwrites its run method.
  2. New An instance that starts a new thread with the start method

The sample

package com.thread; public class FirstThreadTest extends Thread{ int i = 0; // Override the run method. The body of the run method is the field body. Public voidrun()  
        {  
            for(; i<100; i++){ System.out.println(getName()+""+i);  
              
            }  
        }  
        public static void main(String[] args)  
        {  
            for(int i = 0; i< 100; i++) { System.out.println(Thread.currentThread().getName()+":"+i);  
                if(i==20) { new FirstThreadTest().start(); new FirstThreadTest().start(); }}}}Copy the code

extension

The difference between the start and run methods is that the start method starts a new thread and makes it ready. The run method simply executes the instance method in the current thread; calling the run method directly does not start a new thread.

2. Implement the Runnable interface

Usage:

  1. Implement the Runnable interface and override the run method of the class.
  2. New Creates an instance of the Thread object as the target of the Thread
  3. Call the start method of Thread

The sample

    package com.thread;  
      
    public class RunnableThreadTest implements Runnable  
    {  
      
        private int i;  
        public void run()  
        {  
            for(i = 0; i <100; i++) { System.out.println(Thread.currentThread().getName()+""+i);  
            }  
        }  
        public static void main(String[] args)  
        {  
            for(int i = 0; i < 100; i++) { System.out.println(Thread.currentThread().getName()+""+i);  
                if(i==20)  
                {  
                    RunnableThreadTest rtt = new RunnableThreadTest();  
                    new Thread(rtt,"New thread 1").start();  
                    new Thread(rtt,"New Thread 2").start(); }}}}Copy the code

3. Implement the Callable interface

use

  1. Create an implementation class for the Callable interface and implement the Call method
  2. Create an instance of the Callable implementation class and wrap it with the FutureTask class
  3. Create the Thread instance using the FutureTask instance as the target
  4. Call the start method of the Thread instance

The sample

package com.thread;  
      
    import java.util.concurrent.Callable;  
    import java.util.concurrent.ExecutionException;  
    import java.util.concurrent.FutureTask;  
      
    public class CallableThreadTest implements Callable<Integer>  
    {  
      
        public static void main(String[] args)  
        {  
            CallableThreadTest ctt = new CallableThreadTest();  
            FutureTask<Integer> ft = new FutureTask<>(ctt);  
            for(int i = 0; i < 100; i++) { System.out.println(Thread.currentThread().getName()+"The value of the loop variable I of+i);  
                if(i==20)  
                {  
                    new Thread(ft,"Thread with return value").start();  
                }  
            }  
            try  
            {  
                System.out.println("Return value of child thread:"+ft.get());  
            } catch (InterruptedException e)  
            {  
                e.printStackTrace();  
            } catch (ExecutionException e)  
            {  
                e.printStackTrace();  
            }  
      
        }  
      
        @Override  
        public Integer call() throws Exception  
        {  
            int i = 0;  
            for(; i<100; i++) { System.out.println(Thread.currentThread().getName()+""+i);  
            }  
            returni; }}Copy the code

contrast

Runnable/Callable compared with Thread

advantage

  1. You can also inherit from other classes by implementing interfaces
  2. Multiple threads can share the same target, suitable for resource sharing

disadvantage

  1. Programming is a little more complicated, and if you want to access the currentThread, you must use the thread.currentthread () method

Difference between Runnable and Callable

  1. Callable can have a return value, Runnable has no return value
  2. The Call () method of the Callable interface allows exceptions to be thrown; Exceptions to the Runnable interface’s run() method can only be digested internally, not continued

Extension:

The Callable interface supports the return of execution results, in which case you need to call the futureTask.get () method, which blocks the main thread until you get the 'future' result; When this method is not called, the main thread does not blockCopy the code