The first inherits the Thread class

Custom class that inherits Thread and overwrites the run() method.

class MyThread1 extends Thread {
    @Override
    public void run() {
        System.out.println("The first way Thread"+ Thread.currentThread().getName()); }} public class Test {public static void main(String[] args) throws Exception {// Mysread1 = new MyThread1(); thread1.start(); MyThread1 thread1 = new MyThread1(); thread1.start(); MyThread1 thread2 = new MyThread1(); thread2.setName("zz"); thread2.start(); Thread_0 thread_1 // Thread_1 thread_1 // thread_zz //Copy the code

The second implements the Runnable interface

Custom class that implements the run method of the Runnable interface

class MyThread2 implements Runnable {
    @Override
    public void run() {
        System.out.println("Second way Runnable"+ Thread.currentThread().getName()); Public class Test {public static void main(String[] args) throws Exception {// Thread thread2 = new Thread(new MyThread2()); thread2.start(); New Thread(() -> {system.out.println () -> {system.out.println ();"Second way Runnable_Lambda"+ Thread.currentThread().getName()); }).start(); Runnable thread0 Runnable_Lambda thread1}}Copy the code

The third implements the Callable interface

Given that Runnable’s run method does not return a value, we introduced the Callable interface since JDK1.5 with a return value

The constructor in FutureTask is as follows.

public FutureTask(Callable<V> callable) {
    if (callable == null)
        throw new NullPointerException();
    this.callable = callable;
    this.state = NEW;       // ensure visibility of callable
}
Copy the code

The FutureTask class structure is shown as follows.

Because the FutureTask class implements the Runnable interface, threads are implemented as follows.

Public class Test {public static void main(String[] args) throws Exception {// FutureTask<String> thread3 = new FutureTask<>(new MyThread3()); new Thread(thread3).start(); System.out.println(thread3.get()); FutureTask<String> Thread4 = new FutureTask(()->{system.out.println ())"Call");
            return "Lambda callback"; }); new Thread(thread4).start(); System.out.println(thread4.get()); Callable thread0 // Call // Lambda callback}} class MyThread3 implements Callable<String> {@override public String call() throws Exception { System.out.println("Third way Callable" + Thread.currentThread().getName());
        return "Correction"; }}Copy the code

The fourth thread pool

There are two ways to deliver thread pools: Submit or execute. The former accepts Callable interface objects with return values, while the latter does not

Public class Test {public static void main(String[] args) throws Exception {// Mysread1 thread1 = new MyThread1(); thread1.start(); Thread2 = new Thread(new MyThread2()); thread2.start(); Thread3 = new FutureTask<>(new MyThread3()); new Thread(thread3).start(); System.out.println(thread3.get()); / / a fourth way the ExecutorService pool = Executors. NewFixedThreadPool (5); Future<? > submit1 = pool.submit(new MyThread1()); Future<? > submit2 = pool.submit(new MyThread2()); Future<? > submit3 = pool.submit(new MyThread3()); System.out.println(submit1.get() +"" + submit2.get() + ""+ submit3.get()); pool.shutdown(); // Running result // The first method Thread thread0 // the second method Runnable thread-1 // The third method Callable thread-2 // The first method Thread pool-1-thread-1 Runnable pool-1-thread-2 // Callable pool-1-thread-3 // null null callback}}Copy the code

The characteristics of

Either way, start multithreading using the start method in the Thread class. The Runnable interface is different from the Callable interface, which can return parameters of type Object. Thread pools avoid the create and release operations that threads frequently use.

After reading what do not understand welcome to leave a comment below!