创建线程的几种方式(常见)
java中线程创建的几大方式
- 首先需要弄懂,Thread 这个类,该类在package java.lang; 该包下是一些比较常用的,默认自动导入。
- Thread 类的比较重要的两个方法,不要搞混。 start() start方法用于创建线程 run() run方法用是用于并发执行代码逻辑的入口方法。(单独运行run方法是不会新建线程执行的)
- run方法 run方法会判断target对象,调用target.run()方法,该对象是在构造函数中以参数的形式传递进来的。 public Thread(Runnable target) { this(null, target, "Thread-" + nextThreadNum(), 0); } /* What will be run. */ private Runnable target; @Override public void run() { if (target != null) { target.run(); } } 可以看到target对象是Runable类型的,Runable是一个函数式接口,(只有一个方法,也可以不用加@FunctionInterface,符合函数式接口规范就可以) @FunctionalInterface public interface Runnable { public abstract void run(); }
1.编写一个类继承Thread并重写run方法
public static void main(String[] args){ MyThread th = new MyThread(); th.start(); } class MyThread extends Thread{ @Override public void run(){ System.out.println(currentThread().getId()); } }
2.实现Runable接口,作为参数传入Thread
public static void main(String[] args){ MyRunable runable = new MyRunable(); Thread th = new Thread(runable); th.start(); //直接通过lamda表达式 Thread th1 = new Thread(()->{ System.out.println(Thread.currentThread().getId()); }); } //通过类实现Callable接口 class MyRunable implement Runable{ @Override public void run(){ System.out.println(Thread.currentThread().getId()); } }
上面介绍的两中方式都不能接受线程执行完毕的返回结果 下面介绍以下接口和类出场
Callable 只是比Runable多了泛型返回值,和声明异常
@FunctionalInterface public interface Callable<V> { /** * Computes a result, or throws an exception if unable to do so. * * @return computed result * @throws Exception if unable to compute a result */ V call() throws Exception; }
Futrue 该接口定义了以下方法
cancel(boolean):boolean isDone():boolean isCancelled():boolean get():V get(long,TimeUnit):V
FutrueTask 该类实现了Runable 和 Futrue 扮演者一座桥梁的角色,因为我们前面了解到,Thread需要一个Runable对象作为target。而Futrue接口可以获取返回值。所以FutrueTask 实现Runable的run方法实际就是调用Callable的call方法。
3.通过Callable 和 FutrueTask配合实现可接收返回值
public static void main(String[] args){ Callable callable = new Callable<String>(){ @Override public String call() throws Exception{ System.out.println(Thread.currentThread().getId()); return "have a return" } }; FutrueTask futrueTask = new FutrueTask(callable); new Thread(futrueTask).start(); //String res = futrueTask.get(); //以阻塞的方式获取结果 //System.out.println("返回结果"+res); try{ String res = futrueTask.get(10,TimeUnit.SECONDS); //限定指定的时间内,没有获取到结果则会抛出TimeoutException异常 System.out.println("返回结果"+res); } catch(TimeoutException|ExecutionException e){ if(e instanceof TimeoutException) { System.out.println("没有在指定的时间内指定完毕,取消任务,中断线程"); futrueTask.cancel(ture); //会不会调用interrupt() 中断线程 } } }
上一篇:
通过多线程提高代码的执行效率例子
下一篇:
新下载好的idea双击没反应 打不开