创建一个线程的几种方式
创建线程的方式总体可以分为两大类:一个是依赖于Thread类, 一个是依赖于线程池。
其中依赖于Thread类的创建方式如下:
package concurrent.newThread; import java.util.concurrent.*; public class NewThreadByThread { public static void main(String[] args) throws ExecutionException, InterruptedException { //1 传入Runnable接口,lambda表达式的实现 Thread thread = new Thread(()->{ System.out.println("new thread by lambda implement runnable interface"); }); thread.start(); //1.1 传入Runnable接口的实现类NewThreadImplementRunnable, 注意该类并不是线程类 class NewThreadImplementRunnable implements Runnable { @Override public void run() { System.out.println("new thread by implement runnable interface"); } } NewThreadImplementRunnable tmpRunnableClass = new NewThreadImplementRunnable(); Thread thread2 = new Thread(tmpRunnableClass); thread2.start(); //2 通过继承Thread类并重写run方法 class NewThreadExtendsThread extends Thread { @Override public void run() { System.out.println("new thread by extend thread and override run method"); } } Thread thread1 = new NewThreadExtendsThread(); thread1.start(); //3 有返回值,传入callable接口的实现类,并利用FutureTask接受消息 class ImplementCallable implements Callable{ @Override public String call() throws Exception { System.out.println("new thread by implement callable interface"); return "call method return finished"; } } //FutureTask是一个实现Runnable接口的类,FutureTas内部有一个成员变量将callable接口的返回值保存起来,并通过提供get方法获取 FutureTask futureTask = new FutureTask<>(new ImplementCallable()); Thread thread3 = new Thread(futureTask); thread3.start(); System.out.println(futureTask.get()); } }
测试结果:
new thread by lambda implement runnable interface
new thread by implement runnable interface
new thread by extend thread and override run method
new thread by implement callable interface
call method return finished
依赖于线程池的创建方式如下:
package concurrent.newThread; import java.util.concurrent.*; public class NewThreadByExecutorPool { public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService executorService = Executors.newFixedThreadPool(3); //1 无返回值,传入Runnable接口的lambda表达式实现 executorService.submit(()->{ System.out.println("new thread by lambda implement runnable interface"); }); //1.1 无返回值 传入Runnable接口的实现类NewThreadImplementRunnable, 注意该类并不是线程类 class NewThreadImplementRunnable implements Runnable { @Override public void run() { System.out.println("new thread by class implement runnable interface"); } } executorService.submit(new NewThreadImplementRunnable()); //2.1 有返回值,通过线程池,传入实现callable接口的lambda表达式 Futurefuture = executorService.submit(()->{ System.out.println("new thread by executorService and lambda implement callable method"); return "second: call method return finished"; }); System.out.println(future.get()); //2.2 有返回值,通过线程池,传入Callable的实现类 class ImplementCallable1 implements Callable { @Override public String call() throws Exception { System.out.println("new thread by executorService and class implement callable"); return "first: call method return finished"; } } future = executorService.submit(new ImplementCallable1()); System.out.println(future.get()); } }
测试结果:
new thread by lambda implement runnable interface
new thread by class implement runnable interface
new thread by executorService and lambda implement callable method
second: call method return finished
new thread by executorService and class implement callable
first: call method return finished