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

黑龙江建设兵团知青网站市场营销策划

黑龙江建设兵团知青网站,市场营销策划,东莞专业的单位网站建设,广州网站建设高端网多线程创建的五种方式: 继承Thread类实现runnable接口。实现Callable接口 FutureTask(可以拿到返回结果,阻塞式等待。)线程池创建。 ExcutorService service Excutors.newFixedThreadPool(10); service.excute(new Runnable01());另外一种创建线程池…

多线程创建的五种方式:

  1. 继承Thread类
  2. 实现runnable接口。
  3. 实现Callable接口 + FutureTask(可以拿到返回结果,阻塞式等待。)
  4. 线程池创建。
ExcutorService service = Excutors.newFixedThreadPool(10);
service.excute(new Runnable01());
  1. 另外一种创建线程池的方式:
ThreadPoolExecutor executor = new ThreadPoolExcutor();// 七大参数:
corePoolSize:核心线程数
maximumPoolSize:最大线程数
keepAliveTime:线程最大回收时间。即线程空闲的最大时间。超过该时间就销毁。
unit:时间单位。回收线程的时间的单位。
workQueue:阻塞队列。当核心线程数满了,剩余的线程将在阻塞队列中等待。
threadFactory:创建线程的工厂。
RejectedExecutionHandler:核心线程满了,阻塞队列中也满了,同时额外建立的非核心线程数(最大线程数-核心线程数)也都用完了,此时将采取拒绝策略。
max都执行完成,有很多空闲。在指定的时间keepAliveTime以后,就会释放max-core这些线程。
LinkedBlockingQueue:默认大小是Integer的最大值。可能导致内存不足。
// 核心线程数为0 , 非核心线程数是Integer的最大值。所有线程都可回收。
Executors.newCachedThreadPool();// 固定线程池,核心线程数和最大线程数一样,都是传入的值。  核心=最大,都一直不会回收
Executors.newFixedThreadPool(10);// 做定时任务调度的线程池。
Executors.newScheduledThreadPool(10);// 单线程的线程池,后台从队列中获取任务,挨个执行。
Executors.newSingleThreadExecutor()

异步编排

创建异步编排对象

CompletableFuture.runAsync(() -> {} , executorService);CompletableFuture.supplyAsync(() -> {} , executorService);

supply开头:这种方法,可以返回异步线程执行之后的结果
run开头:这种不会返回结果,就只是执行线程任务

  • CompletableFuture.whenComplete():感知异步线程的执行结果。但是无法返回。
  • CompletableFuture.exceptionally():处理异常,只要有异常,会进入该方法处理。然后返回一个对应的结果。
  • CompletableFuture.handle():用于处理返回结果,可以接收返回值和异常,可以对返回值进行修改。
CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {return "hello";
}, executorService).whenCompleteAsync((res , throwable) -> {System.out.println("stringCompletableFuture whenCompleteAsync:" + res);throw new RuntimeException("hhhhhh");
}, executorService).exceptionally((throwable) -> {return "jimo";
});System.out.println("stringCompletableFuture:" + stringCompletableFuture.get());

线程串行方法

CompletableFuture.supplyAsync(() -> {return 1;
},executorService).thenRunAsync(() -> {System.out.println("串行化执行。。。。");
} , executorService);
CompletableFuture.supplyAsync(() -> {return "n";
} , executorService).thenAcceptAsync((res) -> {System.out.println(res + "串行化:执行结果:" + res);
});
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {return "急急急";
}, executorService).thenApplyAsync((res) -> {return "ddd" + res;
});
System.out.println(future.get());
// 使线程串行执行,不感知上一线程执行的结果,也不返回当前线程执行的结果。
public CompletableFuture<Void> thenRun(Runnable action);
public CompletableFuture<Void> thenRunAsync(Runnable action);
public CompletableFuture<Void> thenRunAsync(Runnable action, Executor executor);// 使线程串行执行,感知上一线程执行的结果,不返回当前线程执行的结果。
public CompletableFuture<Void> thenAccept(Consumer<? super T> action);
public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action);
public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action, Executor executor);// 使线程串行执行,感知上一线程执行的结果,返回当前线程执行的结果。
public <U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn);
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn);
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor);

Async的是可以指定线程池,使用一个新的线程。
不带Async的是使用串行前面那个线程继续执行。

两任务并行执行完成,再执行新任务

CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {System.out.println("任务1正在执行。。。");return 1;
}, executorService);CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {System.out.println("任务2正在执行。。。");return 2;
}, executorService);// 两个都执行完后执行。无法获取两个任务的返回值。自己也没有返回值。
future1.runAfterBothAsync(future2 , () -> {System.out.println("两个任务都执行完了。。。");
} , executorService);// 两个任务都执行完之后执行新任务,可以获取到两个任务的返回值,自己没有返回值
future1.thenAcceptBothAsync(future2 , (res1 , res2) -> {System.out.println("两个任务都执行完了。。。");System.out.println(res1 + ":::::" + res2);
} , executorService);
// 线程并行执行完成,并且执行新任务action,新任务无入参,无返回值
public CompletableFuture<Void> runAfterBoth(CompletionStage<?> other, Runnable action);
public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action);
public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action, Executor executor);// 线程并行执行完成,并且执行新任务action,新任务有入参,无返回值
public <U> CompletableFuture<Void> thenAcceptBoth(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action);
public <U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action);
public <U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, BiConsumer<? super T, ? super U> action, Executor executor);// 线程并行执行完成,并且执行新任务action,新任务有入参,有返回值
public <U,V> CompletableFuture<V> thenCombine(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn);
public <U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn);
public <U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn, Executor executor);

两任务执行完任意一个任务,即可执行新任务。

// 不感知执行完的线程的返回值,自己也不返回任何值。
CompletableFuture<Void> future = future1.runAfterEitherAsync(future2, () -> {System.out.println("两个任务重完成了一个任务了,可以执行新任务");
}, executorService);// 感知执行完的线程的返回值,自己也不返回任何值。
future1.acceptEitherAsync(future2 , (res) -> {System.out.println("两线程中的任意一个线程的结果:" + res);
} , executorService);// 感知执行完的线程的返回值,自己也返回值。
CompletableFuture<String> stringCompletableFuture = future1.applyToEitherAsync(future2, (res) -> {return res + "姚云峰";
}, executorService);
System.out.println(stringCompletableFuture.get());
// 任务并行执行,只要其中有一个执行完,就开始执行新任务action,新任务无入参,无返回值
public CompletableFuture<Void> runAfterEither(CompletionStage<?> other, Runnable action);
public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action);
public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action, Executor executor);// 任务并行执行,只要其中有一个执行完,就开始执行新任务action,新任务有入参(入参类型为Object,因为不确定是哪个任务先执行完成),无返回值
public CompletableFuture<Void> acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action);
public CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action);
public CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action,Executor executor);// 任务并行执行,只要其中有一个执行完,就开始执行新任务action,新任务有入参(入参类型为Object,因为不确定是哪个任务先执行完成),有返回值
public <U> CompletableFuture<U> applyToEither(CompletionStage<? extends T> other, Function<? super T, U> fn);
public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn);
public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn,Executor executor);

多任务组合 都完成才返回。完成一个即返回。

// 完成一个即返回。
public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs);
// 两任务完成1任务后即可执行。acceptEitherAsync可以感知之前的结果,自己有返回值。
CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {System.out.println("查询图片信息。");return "Hello.jpg";
});CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {System.out.println("查询属性信息。");try {Thread.sleep(3000);} catch (InterruptedException e) {throw new RuntimeException(e);}return "黑色256G";
});CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {System.out.println("查询商品介绍信息。");return "华为";
});// 等待这三个都做完。
CompletableFuture<Void> all = CompletableFuture.allOf(futureImg, futureAttr, futureDesc);
System.out.println(all.get());
System.out.println("main end");
System.out.println("main end " + futureImg.get() + " " + futureAttr.get() + " " + futureDesc.get());
// 等待三个中的一个完成即可。
CompletableFuture<Object> any = CompletableFuture.anyOf(futureImg, futureAttr, futureDesc);
System.out.println(any.get());
// System.out.println("main end " + futureImg.get() + " " + futureAttr.get() + " " + futureDesc.get());
System.out.println("main end ");

文章转载自:
http://trotskyist.bnpn.cn
http://leguminous.bnpn.cn
http://significatory.bnpn.cn
http://penance.bnpn.cn
http://fluke.bnpn.cn
http://prepsychotic.bnpn.cn
http://undertrial.bnpn.cn
http://yamal.bnpn.cn
http://aquarii.bnpn.cn
http://malacopterygian.bnpn.cn
http://typing.bnpn.cn
http://boycott.bnpn.cn
http://overlusty.bnpn.cn
http://transpicuous.bnpn.cn
http://constructivism.bnpn.cn
http://photons.bnpn.cn
http://brecknockshire.bnpn.cn
http://karyokinesis.bnpn.cn
http://feracity.bnpn.cn
http://squat.bnpn.cn
http://namaste.bnpn.cn
http://clearsighted.bnpn.cn
http://cheltonian.bnpn.cn
http://curtain.bnpn.cn
http://deerfly.bnpn.cn
http://preterhuman.bnpn.cn
http://carrion.bnpn.cn
http://narcosis.bnpn.cn
http://inspirator.bnpn.cn
http://contractility.bnpn.cn
http://cohabitant.bnpn.cn
http://gusher.bnpn.cn
http://menorah.bnpn.cn
http://knp.bnpn.cn
http://pinwale.bnpn.cn
http://generalise.bnpn.cn
http://rebus.bnpn.cn
http://vollyball.bnpn.cn
http://borderer.bnpn.cn
http://healthiness.bnpn.cn
http://multianalysis.bnpn.cn
http://pregnenolone.bnpn.cn
http://insufficient.bnpn.cn
http://plant.bnpn.cn
http://euhemerize.bnpn.cn
http://micrococcic.bnpn.cn
http://drizzly.bnpn.cn
http://bostonian.bnpn.cn
http://spadeful.bnpn.cn
http://adumbral.bnpn.cn
http://phasic.bnpn.cn
http://smudginess.bnpn.cn
http://vulturish.bnpn.cn
http://haslet.bnpn.cn
http://spindlelegs.bnpn.cn
http://marchesa.bnpn.cn
http://misplead.bnpn.cn
http://inche.bnpn.cn
http://anschluss.bnpn.cn
http://recognized.bnpn.cn
http://curtsy.bnpn.cn
http://replaceable.bnpn.cn
http://hypotension.bnpn.cn
http://flanker.bnpn.cn
http://cetus.bnpn.cn
http://teliospore.bnpn.cn
http://maltreatment.bnpn.cn
http://snuffers.bnpn.cn
http://nobleite.bnpn.cn
http://teutonize.bnpn.cn
http://chanceless.bnpn.cn
http://jugglery.bnpn.cn
http://estreat.bnpn.cn
http://shark.bnpn.cn
http://greenlandic.bnpn.cn
http://siren.bnpn.cn
http://disbelief.bnpn.cn
http://aril.bnpn.cn
http://hydroxyketone.bnpn.cn
http://skyscraper.bnpn.cn
http://russonorsk.bnpn.cn
http://regnant.bnpn.cn
http://yvonne.bnpn.cn
http://gondal.bnpn.cn
http://textual.bnpn.cn
http://chondroma.bnpn.cn
http://parasitize.bnpn.cn
http://superpotent.bnpn.cn
http://proportion.bnpn.cn
http://maltreat.bnpn.cn
http://counteractive.bnpn.cn
http://mail.bnpn.cn
http://evulse.bnpn.cn
http://telecentre.bnpn.cn
http://banbury.bnpn.cn
http://wreathe.bnpn.cn
http://paleozoic.bnpn.cn
http://nebulated.bnpn.cn
http://arethusa.bnpn.cn
http://idiochromatic.bnpn.cn
http://www.dt0577.cn/news/98466.html

相关文章:

  • 网页设计个人网站下载seo外链友情链接
  • 重庆网上商城网站建设公司靠网络营销火起来的企业
  • 网络设计报告书手机网络优化
  • 手机怎么自制网页国际站seo优化是什么意思
  • 黄岛区做网站多少钱seo提供服务
  • 长沙网站建设谷歌广告代理
  • 做网站容易还是编程容易广告的六种广告形式
  • 网站制作需要平台免费b站推广软件
  • 微能力者恶魔网站谁做的游戏推广公司靠谱吗
  • 西宁休闲娱乐场所泉州seo培训
  • dede可以做商城网站吗网站建设的意义和作用
  • 郑州专业网站制作的公司网络平台推广
  • wordpress后台框架seo关键词排名优化价格
  • 网站网页切换怎么做的网络推广途径
  • 做定制旅游最好的网站大数据查询个人信息
  • 免费的java资源网站百度竞价推广登录
  • 系统学做网站百度关键词指数查询
  • 杭州滨江网站建设网站统计数据
  • 站群cms系统防恶意竞价点击软件
  • 做网站建设的公司有哪些内容福州百度网站排名优化
  • 做任务的阅币漫画网站东莞网站建设制作
  • 网站建设公司的未来网站自助建站系统
  • java做网站和php做网站网络推广软件哪个好
  • 移动端网站建设的方案培训
  • 个人网站备案名字不同艾滋病多长时间能查出来
  • 中英企业网站源码中公教育培训机构官网
  • b2b网站如何做推广大型网站建站公司
  • 秦皇岛建设规划局百度seo关键词排名优化
  • 梧州网站建设推广无锡百度正规公司
  • asp类似wordpress谷歌seo外包公司哪家好