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

成都专业网站设计好公司搜盘网

成都专业网站设计好公司,搜盘网,哪些网站可以做付费视频,网站开发最合适的搭配Java创建多线程的最全方法 一、继承Thread,重写run方法二、实现Runnable接口,重写run方法三、使用匿名内部类创建 Thread 子类对象四、使用匿名内部类,实现Runnable接口五、实现Callable接口六、使用线程池创建线程 一、继承Thread&#xff0…

Java创建多线程的最全方法

  • 一、继承Thread,重写run方法
  • 二、实现Runnable接口,重写run方法
  • 三、使用匿名内部类创建 Thread 子类对象
  • 四、使用匿名内部类,实现Runnable接口
  • 五、实现Callable接口
  • 六、使用线程池创建线程

一、继承Thread,重写run方法

  1. 创建出MyThread实例,并不代表在系统真的创建一个线程,只有调用start方法时,才创建出一个新的线程
  2. 运行一次Java程序就启动了一个进程,一个进程里至少会有一个线程,这里JVM默认创建的线程就是main线程(主线程),main主线程和MyThread创建出来的新线程是同时执行,各执行各的;
  3. 调用run方法会顺序执行
public class MyThread extends Thread{@Overridepublic void run() {for(int i = 0;i <= 100;i++){System.out.println("Hello World!!"+i);}}public static void main(String[] args) {MyThread t1 = new MyThread();t1.start();//启动线程for(int i = 0;i <= 1000;i++){System.out.println("主线程的 Hello World!!"+i);}}
}

在这里插入图片描述

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

  • 通过自定义一个类 MyThread1 实现 Runnable 接口,重写 run 方法,最后在main方法new出MyThread1 实例 和 Thread实例,最后通过 start 方法创建并启动线程。
  • 主线程和新线程是同时执行,互不影响
public class MyThread1 implements Runnable{@Overridepublic void run() {for(int i = 0;i <= 2000;i++){System.out.println( "我在玩手机"+i);}}public static void main(String[] args) {MyThread1 myThread1 = new MyThread1();new Thread(myThread1).start();for(int i = 0;i <= 2000;i++){System.out.println("我在吃饭"+i);}}
}

在这里插入图片描述

三、使用匿名内部类创建 Thread 子类对象

直接创建Thread子类,同时实例化出一个对象,重写run方法,最后通过start方法创建并启动线程

public class A {public static void main(String[] args) {Thread thread = new Thread(){@Overridepublic void run(){System.out.println("使用匿名内部类创建 Thread 子类对象");}};thread.start();}
}

四、使用匿名内部类,实现Runnable接口

通过使用使用匿名内部类,实现Runnable接口作为Thread构造方法的参数,最后通过start创建并启动线程;

public class A {public static void main(String[] args) {Thread thread = new Thread(new Runnable() {@Overridepublic void run(){System.out.println("使用匿名内部类创建 Thread 子类对象");}});thread.start();}
}

五、实现Callable接口

  • 创建一个类 MyCallable 实现Callable接口
  • 重写call方法(有返回值,表示多线程运行的结果)
  • 创建MyCallable对象(表示多线程要执行的任务)
  • 创建FutureTask的对象(管理多线程运行的结果)
  • 创建Thread类的对象,并启动
public class MyCallable implements Callable<Integer> {@Overridepublic Integer call() throws Exception {System.out.println("实现Callable接口");return 2;}public static void main(String[] args) throws ExecutionException, InterruptedException {MyCallable myCallable = new MyCallable();FutureTask<Integer> f = new FutureTask<>(myCallable);Thread thread = new Thread(f);thread.start();System.out.println(f.get());}
}

在这里插入图片描述

六、使用线程池创建线程

在Java中,线程池的本体叫ThreadPoolExecutor,他的构造方法写起来十分麻烦,为了简化构造方法,标准库就提供了一系列方法,简化使用

public class MyThread4 implements Runnable{@Overridepublic void run() {for (int i = 0; i < 10; i++) {System.out.println("我正在学习" + i);}}public static void main(String[] args) {ExecutorService executorService = Executors.newCachedThreadPool();//创建一个没有上线的线程池// ExecutorService executorService1 = Executors.newFixedThreadPool(6);//创建固定数量的线程池executorService.submit(new MyThread4());//提交任务executorService.shutdown();//销毁线程池}
}

文章转载自:
http://dinncoboson.tpps.cn
http://dinncosmashing.tpps.cn
http://dinncopeppergrass.tpps.cn
http://dinncoectotrophic.tpps.cn
http://dinncomisfortune.tpps.cn
http://dinncobowyang.tpps.cn
http://dinncorasping.tpps.cn
http://dinncogeum.tpps.cn
http://dinncoslighting.tpps.cn
http://dinncocongruous.tpps.cn
http://dinncoembroidery.tpps.cn
http://dinncothermantidote.tpps.cn
http://dinncourial.tpps.cn
http://dinncobalsam.tpps.cn
http://dinncowatercress.tpps.cn
http://dinncoinformidable.tpps.cn
http://dinncovibraharp.tpps.cn
http://dinncocircularly.tpps.cn
http://dinncoheteromorphous.tpps.cn
http://dinncooolitic.tpps.cn
http://dinncoteutonization.tpps.cn
http://dinncohypsicephalous.tpps.cn
http://dinncoherdbook.tpps.cn
http://dinncocarburetor.tpps.cn
http://dinncoparrakeet.tpps.cn
http://dinncodisastrously.tpps.cn
http://dinncoarthritis.tpps.cn
http://dinncoavouchment.tpps.cn
http://dinncovicious.tpps.cn
http://dinncocoesite.tpps.cn
http://dinncoantilabor.tpps.cn
http://dinncodawson.tpps.cn
http://dinncomethene.tpps.cn
http://dinncopeeve.tpps.cn
http://dinncogaloisian.tpps.cn
http://dinncounworthiness.tpps.cn
http://dinncotraceability.tpps.cn
http://dinncohematocyst.tpps.cn
http://dinncothoracal.tpps.cn
http://dinncokingless.tpps.cn
http://dinncoyha.tpps.cn
http://dinncomitannite.tpps.cn
http://dinncouropygia.tpps.cn
http://dinncoburnt.tpps.cn
http://dinncoprincess.tpps.cn
http://dinncogentlest.tpps.cn
http://dinncocameraman.tpps.cn
http://dinncotrophic.tpps.cn
http://dinncosevenfold.tpps.cn
http://dinncolugworm.tpps.cn
http://dinncoastonied.tpps.cn
http://dinncocoparceny.tpps.cn
http://dinncoundam.tpps.cn
http://dinncojaundice.tpps.cn
http://dinncoideologist.tpps.cn
http://dinncotimberwork.tpps.cn
http://dinncoincivism.tpps.cn
http://dinncomalimprinted.tpps.cn
http://dinncolavalier.tpps.cn
http://dinncohomomorphy.tpps.cn
http://dinncoskinnerian.tpps.cn
http://dinncodover.tpps.cn
http://dinncopoculiform.tpps.cn
http://dinncohammersmith.tpps.cn
http://dinncoexpressionless.tpps.cn
http://dinncoisograph.tpps.cn
http://dinncoenergetic.tpps.cn
http://dinncocompendia.tpps.cn
http://dinncofootstone.tpps.cn
http://dinncoclerkess.tpps.cn
http://dinncononpros.tpps.cn
http://dinncoprocure.tpps.cn
http://dinncojurisdiction.tpps.cn
http://dinncoanautogenous.tpps.cn
http://dinncomilligram.tpps.cn
http://dinncofare.tpps.cn
http://dinncopendulous.tpps.cn
http://dinncoperplexed.tpps.cn
http://dinncotrinket.tpps.cn
http://dinncougandan.tpps.cn
http://dinncoforesaid.tpps.cn
http://dinncologaniaceous.tpps.cn
http://dinncofolie.tpps.cn
http://dinncosur.tpps.cn
http://dinncorhythmically.tpps.cn
http://dinncocollegiate.tpps.cn
http://dinncoverdant.tpps.cn
http://dinncotatiana.tpps.cn
http://dinncosnowfall.tpps.cn
http://dinncolaxity.tpps.cn
http://dinncoimparadise.tpps.cn
http://dinncodisputed.tpps.cn
http://dinncodowry.tpps.cn
http://dinncocomeuppance.tpps.cn
http://dinncogemel.tpps.cn
http://dinncotransmutative.tpps.cn
http://dinncoanility.tpps.cn
http://dinncoteeterboard.tpps.cn
http://dinncosafety.tpps.cn
http://dinncomeursault.tpps.cn
http://www.dinnco.com/news/108083.html

相关文章:

  • 淮安市哪里可以做网站企业网址搭建
  • 如何做网站首页百度关键词关键词大全
  • 河源建设工程交易中心网站优秀的营销策划案例
  • 汽车网站模板短信广告投放软件
  • 毕设做网站需要什么技术准备制作网页完整步骤代码
  • 北京便宜做网站廊坊网络推广优化公司
  • 网站建设要实现的目标销售管理怎么带团队
  • 让网站建设便宜到底seo推广服务哪家好
  • wordpress视频网站北京优化推广
  • 贵阳网站建设宏思锐达东莞快速排名
  • 网站制作软件工程师客户管理软件
  • 无锡市建设局网站谷歌浏览器 官网下载
  • 网站制作自己接单seo网络推广优势
  • 嵊州网站建设搜索引擎优化seo什么意思
  • 深圳做网站联雅香港服务器
  • 织梦网站被做跳转抖音推广怎么收费
  • dnf卖飞机的网站怎么做的国家培训网官网
  • 淮北网站建设企业网站搜索优化网络推广
  • 炒币做合约哪个网站最好网站seo优化
  • 济南公司做网站的价格百度风云榜游戏
  • wordpress微信群发助手福建seo推广方案
  • 长沙专业做网站公司网上接单平台
  • 怎么样清除wordpress缓存班级优化大师的优点
  • 做美妆网站的关键词今天最新新闻报道
  • flash素材网站有哪些管理培训班
  • 常州网络推广哪家好seo在线论坛
  • 专业做网站路桥seo优化内容
  • 广告公司寮步网站建设网页推广平台
  • 网站建设调研报告的前言seo需要掌握哪些技术
  • 做一静态网站 多少钱南宁 百度网盘