当前位置: 首页 > news >正文

网站添加外链客户关系管理名词解释

网站添加外链,客户关系管理名词解释,成都设计公司视频制作,福州建设注册中心网站要和 1.6Runnable 和 Thread 类,几种创建线程方式比较。创建线程几种方式呢?5 种继承 Thread 类Runnable 接口Java 5 后,实现 Callable 接口Java 线程池ComplatableFuture5.1 Callable 和 Runnable 区别有三个区别:具体方法不同&a…

要和 1.6Runnable 和 Thread 类,几种创建线程方式比较。

创建线程几种方式呢?5 种

  1. 继承 Thread 类
  2. Runnable 接口
  3. Java 5 后,实现 Callable 接口
  4. Java 线程池
  5. ComplatableFuture

5.1 Callable 和 Runnable 区别

有三个区别:

  1. 具体方法不同:一个是run,一个是call
  2. Runnable没有返回值;Callable可以返回执行结果,是个泛型
  3. Callable接口的call()方法允许抛出异常;Runnable的run()方法异常只能在内部消化,不能往上继续抛
FutureTask futureTask = new FutureTask(new Callable());
Thread t = new Thread(FutureTask);
t.start();
Thread t = new Thread(new Runnable());
t.start();

5.2 Callable 创建线程

  • new Thread()是不接收Callable对象的,只能接收Runnable对象,那到底该如何使用Callable呢?

这时候借助一个FutureTask -> RunnableFuture -> Runnable

所以其实根本上,还是因为它实现了RunnableFuture接口,这个接口继承于Runnable接口

所以肯定是可以运行起来了!

new Thread(new FutureTask<>(new CallableFun()), "Callable").start();

class CallableFun implements Callable<Integer> {@Overridepublic Integer call() throws Exception {Thread.sleep(6);System.out.println(Thread.currentThread().getName() + "Callable is running!");return 666;}
}class RunnableFun implements Runnable {@Overridepublic void run() {try {Thread.sleep(10);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(Thread.currentThread().getName() + "Runnable is running!");}
}public class CallableDemo {public static void main(String[] args) {new Thread(new RunnableFun(), "RunableFun").start();new Thread(new FutureTask<>(new CallableFun()), "Callable").start();}
}

  • 那么如何处理返回值呢?

使用Thread进行过去,发现没有那样的方法

这里使用FutureTask.get进行获取返回值,因为嵌在它里面,也可以理解

public class CallableDemo {public static void main(String[] args) throws ExecutionException, InterruptedException {new Thread(new RunnableFun(), "RunableFun").start();FutureTask<Integer> futureTask = new FutureTask<>(new CallableFun());new Thread(futureTask, "CallableFun").start();System.out.println("获取到结果之前");Integer res = futureTask.get();System.out.println("获取到结果之后" + res);}
}

主线程,在 Callable 执行完之后才打印值,就是等他们执行完才得到返回值,get 方法会阻塞主线程。

  • 因为Callable接口有throw可以处理异常,所以需要抛出去
  • 需要将睡眠时间更改一下
class CallableFun implements Callable<Integer> {@Overridepublic Integer call() throws Exception {Thread.sleep(1000);System.out.println(Thread.currentThread().getName() + " Callable is running!");return 666;}
}class RunnableFun implements Runnable {@Overridepublic void run() {try {Thread.sleep(500);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(Thread.currentThread().getName() + " Runnable is running!");}
}

5.3 FutureTask使用

一个FutureTask对象之后就只有一个结果输出,虽然是两个线程

public static void main(String[] args) throws ExecutionException, InterruptedException {FutureTask<Integer> futureTask = new FutureTask<>(new CallableFun());Thread t1 = new Thread(futureTask, "线程 1");Thread t2 = new Thread(futureTask, "线程 2");t1.start();t2.start();System.out.println("获取到结果之前");Integer res = futureTask.get();System.out.println("获取到结果之后" + res);
}

2 个FutureTask对象之后才会有 2 个结果输出

class CallableFun implements Callable<Integer> {@Overridepublic Integer call() throws Exception {Thread.sleep(500);System.out.println(Thread.currentThread().getName() + " Callable is running!");return new Random().nextInt(1000);}
}public static void main(String[] args) throws ExecutionException, InterruptedException {FutureTask<Integer> futureTask1 = new FutureTask<>(new CallableFun());FutureTask<Integer> futureTask2 = new FutureTask<>(new CallableFun());Thread t1 = new Thread(futureTask1, "线程 1");Thread t2 = new Thread(futureTask2, "线程 2");t1.start();t2.start();System.out.println("获取到结果之前");Integer res1 = futureTask1.get();Integer res2 = futureTask2.get();System.out.println("获取到结果之后" + res1);System.out.println("获取到结果之后" + res2);
}

FutureTask的方法

  • get:阻塞主线程
  • isDone:判断异步线程是否执行完毕
  • isCancelled:是否被取消了
  • cancel
    • true:打断子线程执行,并且抛出异常,无返回结果
    • false:不打断子线程执行,并且抛出异常,无返回结果

isDone方法:就是另一种判断是否get完成的方法

class CallableFun implements Callable<Integer> {@Overridepublic Integer call() throws Exception {for (int i = 0; i < 10; i++) {Thread.sleep(500);System.out.println(Thread.currentThread().getName() + " Callable is running!");}return new Random().nextInt(1000);}
}public class CallableDemo {public static void main(String[] args) throws ExecutionException, InterruptedException {FutureTask<Integer> futureTask = new FutureTask<>(new CallableFun());new Thread(futureTask, "线程 1").start();System.out.println();;System.out.println("get 前:" + futureTask.isDone());Integer res = futureTask.get();System.out.println("get 后:" + futureTask.isDone());System.out.println("获取到结果:" + res);}
}

isCancelled、cancel方法

这里Thread.sleep是为了让主线程睡眠,子线程继续执行futureTask任务,来判断是否让子线程继续执行。

package CallableInter;import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;class CallableFun implements Callable<Integer> {@Overridepublic Integer call() throws Exception {for (int i = 0; i < 10; i++) {Thread.sleep(500);System.out.println(Thread.currentThread().getName() + " Callable is running!");}return new Random().nextInt(1000);}
}public class CallableDemo {public static void main(String[] args) throws ExecutionException, InterruptedException {//        new Thread(new RunnableFun(), "RunableFun").start();FutureTask<Integer> futureTask = new FutureTask<>(new CallableFun());new Thread(futureTask, "线程 1").start();System.out.println("get 前是否 Done:" + futureTask.isDone());System.out.println("get 前取消:" + futureTask.isCancelled());Thread.sleep(3000);futureTask.cancel(false);Integer res = futureTask.get();System.out.println("get 后取消:" + futureTask.isCancelled());System.out.println("get 后是否 Done:" + futureTask.isDone());System.out.println("获取到结果:" + res);}
}

false不打断子线程的执行,会执行结束

http://www.dtcms.com/a/617091.html

相关文章:

  • 不锈钢公司网站源码 网站建设 产品3级分类asp源码微信小程序购物平台
  • php网站怎么样可以做机械设计接单的网站
  • 网站建设验收使用情况公路建设管理办公室网站
  • 自己怎么做网站网页云主机 免费
  • 企业品牌文化建设学习网站wordpress 3.5 官方中文版
  • 怎么呢搜到自己建设的网站河北百度推广电话
  • 陵园网站建设价格厦门网站推广公司哪家好
  • 网站开发 策划书常州做网站哪里好
  • 注册过什么网站基于网站开发小程序
  • 请兼职做企业网站设计怎么收费湖南省建设工程造价管理协会网站
  • 网站ip访问做图表许昌住房建设局网站
  • 网站模板下载大全免费网站的代码
  • 上海高玩seo广州seo排名优化服务
  • 这样做网站推广深圳定制网站制作厂家
  • 天津网架公司怎样优化网络
  • 做外贸有哪些免费的网站wordpress 加微信号
  • 做杂志的模板下载网站网站进行规划与设计
  • 为外国企业做中文网站建设营口市住房建设保障办官方网站
  • 网站开发费税率重庆网站建设项目
  • 厦门网站设计多少钱做视频赚钱的网站有哪些
  • 西安seo站内优化阿里云服务器12元一年
  • 通信工程毕设可以做网站吗黑马程序员c++笔记
  • 中国建设部网站宁波今晨发现1例阳性
  • 东莞站福公司工资注册网站给谁交钱
  • 网站开发所需要的语言公众平台安全助手
  • wordpress网站主机网页设计网站
  • 网站建设在哪里找奉化建设局网站
  • 有哪些开发网站公司上海城乡建设网站证件查询
  • 建设项目环保验收平台网站珠海免费模板建站
  • 成都软件网站开发网站案例分析