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

Java中线程的创建方式

一、继承Thread类,重写run方法

public class MyThread{public static void main(String[] args) {Thread threadDome = new ThreadDome();threadDome.start();}
}class ThreadDome extends Thread{@Overridepublic void run() {for (int i = 0; i < 5; i++) {try {Thread.sleep(500);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println("i = " + i);}}
}

二、实现Runnable接口,重写run方法

public class MyThread{   public static void main(String[] args) {MyRunnable myRunnable = new MyRunnable();new Thread(myRunnable).run();}
}class MyRunnable implements Runnable{@Overridepublic void run() {for (int i = 0; i < 5; i++) {try {Thread.sleep(500);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println("i = " + i);}}
}

三、实现Callable接口,重写call方法

public class MyThread{public static void main(String[] args) throws ExecutionException, InterruptedException             {MyCallable myCallable = new MyCallable();FutureTask futureTask = new FutureTask(myCallable);new Thread(futureTask).start();System.out.println(futureTask.get());}
}class MyCallable implements Callable<Integer>{@Overridepublic Integer call() throws Exception {return 10;}
}

callable相较于runnable接口,可以拥有返回值,并且runnable中程序出现异常只能通过try...catch进行捕获然后处理,callable可以将异常抛出。

四、使用线程池

1、线程池的实现:

1、FixedThreadPool(定长线程池):只有核心线程,线程数量固定,执行完立即回收,任务队列为链表结果的有界队列,控制线程的最大并发数

    @Testpublic void test() throws ExecutionException, InterruptedException {//创建线程池ExecutorService pool = Executors.newFixedThreadPool(5);//使用线程池for (int i = 0; i < 5; i++) {Future<Integer> submit = pool.submit(new Callable<Integer>() {@Overridepublic Integer call() throws Exception {return 10;}});System.out.println(submit.get());}//关闭线程池pool.shutdown();}

2、ScheduledThreadPool(定时线程池):核心线程数量固定,非核心线程数量无限,执行完闲置10s后回收,任务队列为延时阻塞队列,执行定时或周期性任务

    @Testpublic void test02() throws ExecutionException, InterruptedException {//创建线程池ScheduledExecutorService pool = Executors.newScheduledThreadPool(5);//使用线程池for (int i = 0; i < 5; i++) {ScheduledFuture<Integer> schedule = pool.schedule(new Callable<Integer>() {@Overridepublic Integer call() throws Exception {return 10;}}, 1, TimeUnit.SECONDS);System.out.println(schedule.get());}//销毁线程池pool.shutdown();}

3、CachedThreadPool(可缓存线程池):无核心线程,非核心线程数量无限,执行完闲置60s回收,任务队列为不存储元素的阻塞队列,执行大量,耗时少的任务

    @Testpublic void test03() throws ExecutionException, InterruptedException {//创建线程池ExecutorService pool = Executors.newCachedThreadPool();//使用线程池for (int i = 0; i < 5; i++) {Future<Integer> submit = pool.submit(new Callable<Integer>() {@Overridepublic Integer call() throws Exception {return 10;}});System.out.println(submit.get());}//关闭线程池pool.shutdown();}

4、SingleThreadExecutor(单线程化线程池):只有一个核心线程,无非核心线程,执行完立即回收,任务队列为链表结果的有界队列,不适合做并发但可能引起io阻塞及影响ui线程响应的操作,如数据库操作、文件操作等

@Testpublic void test04() throws ExecutionException, InterruptedException {//创建线程池ExecutorService pool = Executors.newSingleThreadExecutor();//使用线程池for (int i = 0; i < 5; i++) {Future<Integer> submit = pool.submit(new Callable<Integer>() {@Overridepublic Integer call() throws Exception {return 10;}});System.out.println(submit.get());}//关闭线程池pool.shutdown();}

2、excute和submit的区别

excute:只能提交Runable类型任务,会直接抛出异常,可以用try、catch来捕获,和普通线程处理方式一致,excute没有返回值

submit:能提交Runable类型任务也能提交Callable类型任务,会吃掉异常,可通过future的get方法将任务执行时的异常重新抛出,submit有返回值,需要返回值的时候必须使用submit

submit最后的任务也是抛给excute方法,提交任务不需要一个结果的话,直接用excute()提高性能

http://www.lryc.cn/news/375027.html

相关文章:

  • 猫头虎推荐20个值得体验的通用大模型
  • Novartis诺华制药社招综合能力性格动机问卷入职测评笔试题库答案及包过助攻
  • Adam优化算法
  • MYSQL 三、mysql基础知识 7(MySQL8其它新特性)
  • git error: does not have a commit checked out fatal: adding files failed
  • Java Websocket分片发送
  • vivado NODE、PACKAGE_PIN
  • JavaEE、SSM基础框架、JavaWeb、MVC(认识)
  • 【漏洞复现】飞企互联-FE企业运营管理平台 treeXml.jsp SQL注入漏洞
  • Android基础-运行时权限
  • postman断言及变量及参数化
  • 安装和使用TrinityCore NPCBot
  • Hvv--知攻善防应急响应靶机--Linux2
  • replaceAll is not a function 详解
  • 如何设置天锐绿盾的数据防泄密系统
  • 003 gitee怎样将默认的私有仓库变成公开仓库
  • Spring框架中的IOC(控制反转)详解
  • Score Matching(得分匹配)
  • 五大维度大比拼:ChatGPT比较文心一言,你的AI助手选择指南
  • 大学课设项目,Windows端基于UDP的网络聊天程序的服务端和客户端
  • 【5.x】ELK日志分析、集群部署
  • 揭秘创业加盟:豫腾助力,发掘商机,共赢未来
  • Linux操作系统以及一些操作命令、安装教程
  • 树莓派4B_OpenCv学习笔记6:OpenCv识别已知颜色_运用掩膜
  • ZSH 配置
  • LogicFlow 学习笔记——5. LogicFlow 基础 主题 Theme
  • Centos7.9使用kubeadm部署K8S集群
  • VB.net调用VC DLL(二)
  • 社团管理系统
  • 网站的文章起到什么作用