Three ways to create threads

1, Inherit Thread class

In java, classes are single inheritance. If you inherit this class, you can't inherit other classes, which increases the coupling of code. Now, interface oriented programming is advocated.

2, Implement the Runnable interface

Compared with method 1, it uses the implementation interface to reduce the coupling between programs, but it has no return value.

3, Implement the Callable interface, pass the Callable instance to FutureTask (FutureTask implements the RunnableFuture interface, and RunnableFuture inherits the Runnable interface)

In the same way as mode 2, threads are created by implementing interfaces, but different from mode 2, return values are available.

Code:

package com.shamgod.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

/**
 * Three ways to create threads
 * @author ShammGod
 *
 */
public class TestThread {
    
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        //inherit Thread class
        Thread t1 = new Thread1();
        t1.start();
        
        //Realization Runnable Interface, creating the instance as a parameter Thread object
        Runnable runnable = new Thread2();
        Thread t2 = new Thread(runnable);
        t2.start();
        
        //Realization Callable Interface, creating the instance as a parameter FutureTask Object, will FutureTask Object created as a parameter Thread object
        Callable callable = new Thread3();
        FutureTask<String> futureTask = new FutureTask<>(callable);
        Thread t3 = new Thread(futureTask);
        t3.start();
        futureTask.get();//call Return value of method
        
        //Realization Callable Interface, passing the instance to the thread pool's submit Method start thread
        ExecutorService threadPool = Executors.newCachedThreadPool();
        Callable<String> task = new Thread3();
        Future<String> future = threadPool.submit(task);
        threadPool.shutdown();
        future.get();//call Return value of method

    }
}
/**
 * 1,Inheriting Thread
 * @author ShammGod
 *
 */
class Thread1 extends Thread{
    @Override
    public void run() {
        System.out.println("I am inherited. Thread Thread created:" + Thread.currentThread().getName());
    }
}

/**
 * 2,Implement the Runnable interface
 * @author ShammGod
 *
 */
class Thread2 implements Runnable{

    @Override
    public void run() {
        // TODO Auto-generated method stub
            System.out.println("I am achieving Runnable Thread created by interface:"+Thread.currentThread().getName());
    }
}

/**
 * 3,Implement Callable interface
 * @author ShammGod
 *
 */
class Thread3 implements Callable<String>{

    @Override
    public String call() throws Exception {
        System.out.println("I am achieving Callable Thread created by interface:"+Thread.currentThread().getName());
        return "Return value";
    }
    
}

 

 

If there is something wrong, please forgive me and welcome criticism and correction.

Keywords: Java Programming

Added by PDXDesigner on Thu, 13 Feb 2020 19:40:23 +0200