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

微信 存储wordpress企业网站seo哪里好

微信 存储wordpress,企业网站seo哪里好,企业展厅设计公司100%正品保障,大学生简历模板线程的实现方式 继承Thread类:void run()方法没有返回值,无法抛异常实现Runnable接口:void run()方法没有返回值,无法抛异常实现Callable接口:V call() throws Exception 返回结果,能够抛异常 实现Callab…

线程的实现方式

  1. 继承Thread类:void run()方法没有返回值,无法抛异常
  2. 实现Runnable接口:void run()方法没有返回值,无法抛异常
  3. 实现Callable接口:V call() throws Exception 返回结果,能够抛异常

实现Callable接口

(1)创建Callable接口的实现类,并实现call()方法,该call()方法将作为线程执行体,并且有返回值。

(2)创建Callable实现类的实例,使用FutureTask类来包装Callable对象,该FutureTask对象封装了该Callable对象的call()方法的返回值。

(3)使用FutureTask对象作为Thread对象的target创建并启动新线程。

(4)调用FutureTask对象的get()方法来获得子线程执行结束后的返回值

任务

四匹马,跑一千米比赛,每匹马的速度通过1~10的随机数来产生,输出哪匹马是冠军

RunTask1.java代码:

import java.util.Random;
import java.util.concurrent.Callable;/*** 跑步任务*/public class RunTask1 implements Callable<Long> {// step/100ms 是否睡着 睡觉时间// 通过方法形参接收参数// 定义属性private int step;private boolean isSleep;private int sleepTime;public RunTask1(int step,boolean isSleep,int sleepTime){this.step=step;this.isSleep=isSleep;this.sleepTime=sleepTime;}@Overridepublic Long call() throws Exception {int distance=0;long start = System.currentTimeMillis();while(true){distance+=step;if (isSleep) {if (distance == 800) { //跑到800米try {System.out.println(Thread.currentThread().getName() + "睡" + sleepTime + "ms");Thread.sleep(sleepTime);} catch (InterruptedException e) {e.printStackTrace();}}}if(distance>=1000){break;}System.out.println(Thread.currentThread().getName() + "跑完" + distance + "米");}try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}return System.currentTimeMillis()-start;}
}

 Test.java代码

import java.util.Random;
import java.util.concurrent.FutureTask;public class Test {public static void main(String[] args) throws Exception {//第一匹马的跑步任务RunTask1 horse1=new RunTask1(new Random().nextInt(10)+1,true,200);FutureTask<Long> horseResult = new FutureTask<>(horse1);Thread t1=new Thread(horseResult,"马1");//第二匹马的跑步任务RunTask1 horse2=new RunTask1(new Random().nextInt(10)+1,true,400);FutureTask<Long> horseResult1 = new FutureTask<>(horse2);Thread t2=new Thread(horseResult1,"马2");//第三匹马的跑步任务RunTask1 horse3=new RunTask1(new Random().nextInt(10)+1,true,5500);FutureTask<Long> horseResult2 = new FutureTask<>(horse3);Thread t3=new Thread(horseResult2,"马3");//第四匹马的跑步任务RunTask1 horse4=new RunTask1(new Random().nextInt(10)+1,false,0);FutureTask<Long> horseResult3 = new FutureTask<>(horse4);Thread t4=new Thread(horseResult3,"马4");t1.start();t2.start();t3.start();t4.start();//统计比赛结果,一定要等main线程执行,否则, Horse1的线程还没跑完,就输出了winner is Horse2if((horseResult.get().longValue() < horseResult1.get().longValue()) && (horseResult.get().longValue() < horseResult2.get().longValue()) && (horseResult.get().longValue() < horseResult3.get().longValue())){System.out.println("winner is "+t1.getName());}else if((horseResult1.get().longValue() < horseResult.get().longValue()) && (horseResult1.get().longValue() < horseResult2.get().longValue()) && (horseResult1.get().longValue() < horseResult3.get().longValue())){System.out.println("winner is "+t2.getName());}else if((horseResult2.get().longValue() < horseResult.get().longValue()) && (horseResult2.get().longValue() < horseResult1.get().longValue()) && (horseResult2.get().longValue() < horseResult3.get().longValue())){System.out.println("winner is "+t3.getName());}else if(horseResult3.get().longValue() < horseResult.get().longValue() && (horseResult3.get().longValue() < horseResult1.get().longValue()) && (horseResult3.get().longValue() < horseResult2.get().longValue())){System.out.println("winner is "+t4.getName());}else System.out.println("平局");}
}

控制台输出,想要结果不一样,可以通过设置哪一匹马会睡眠,从而改变结果,代码还是有不足:

http://www.dt0577.cn/news/54221.html

相关文章:

  • 建网站是怎么造成的重庆seo代理计费
  • 购物分享网站怎么做的企业宣传软文范例
  • 四川掌上电力app下载seo文案范例
  • 网站衣服模特怎么做免费推广网站排行榜
  • 青岛做网站的淘宝指数查询
  • dw简述网站开发流程适合发软文的平台
  • 怎样做直播网站app做外贸网站的公司
  • 制作h5网页软件徐州关键词优化排名
  • 做网站能用微软宁波seo网络推广咨询价格
  • wordpress 视频自适应seo免费系统
  • 成都网站优化公司友情链接购买
  • 来宾网站建设百度广告运营
  • 免费网站建设 免备案郑州网络营销策划
  • 社区网站建设申请报告网络营销与推广
  • 做网站资料准备黄金网站软件app大全下载
  • 上虞网站建设文广网络百度网盘人工申诉电话
  • 哈尔滨网站开发培训东莞今日新闻大事
  • 家装网站模板下载发稿服务
  • 做网站的网页北京seo专员
  • 网站专题页是什么手游推广平台有哪些
  • 织梦手机网站模板杭州推广系统
  • 快递网站推广怎么做太原网络营销公司
  • 国务院关于加强政府网站建设网站建设公司官网
  • 网站制作与防护费用seowhy
  • 旅游商城网站建设seo培训网
  • asp网站上一篇下一篇代码排行榜百度
  • 做网络推广要做网站吗seo互联网营销培训
  • 苏州做网站推广的软文代发价格
  • 高端大气企业网站惠州seo快速排名
  • 建设网站论坛社群营销方案