96SEO 2026-02-19 21:19 0
比如主线程让一个子线程去执行任务#xff0c;子线…Future

实现类定义了操作异步任务执行的一些方法如获取异步任务的执行结果、取消任务的执行、判断任务是否被取消、判断任务执行是否完毕等。
比如主线程让一个子线程去执行任务子线程可能比较耗时启动子线程开始执行任务后主线程就去做其他事情了忙其他事情或者先执行完过了一会才去获取子任务的执行结果或变更的任务状态。
接口可以为主线程开一个分支任务专门为主线程处理耗时和复杂业务。
把这个任务放到异步线程中执行。
主线程继续处理其他任务或者先行结束再通过
Thread(futureTask);t1.start();System.out.println(futureTask.get());}
{method1();System.out.println(------------------);method2();}private
Executors.newFixedThreadPool(3);long
System.currentTimeMillis();FutureTaskString
{TimeUnit.MILLISECONDS.sleep(500);}
over;});executorService.submit(futureTask1);FutureTaskString
{TimeUnit.MILLISECONDS.sleep(300);}
over;});executorService.submit(futureTask2);FutureTaskString
{TimeUnit.MILLISECONDS.sleep(300);}
over;});executorService.submit(futureTask3);long
System.currentTimeMillis();System.out.println(----costTime:
毫秒);System.out.println(Thread.currentThread().getName()
----end);executorService.shutdown();}private
{TimeUnit.MILLISECONDS.sleep(500);}
{TimeUnit.MILLISECONDS.sleep(300);}
{TimeUnit.MILLISECONDS.sleep(300);}
System.currentTimeMillis();System.out.println(----costTime:
毫秒);System.out.println(Thread.currentThread().getName()
{log.debug(Thread.currentThread().getName()
t1);t1.start();log.debug(futureTask.get());log.debug(Thread.currentThread().getName()
方法求结果如果计算没有完成容易导致程序阻塞。
如果不希望等待过长时间希望在等待指定时间后自动结束。
可以使用
{log.debug(Thread.currentThread().getName()
t1);t1.start();log.debug(Thread.currentThread().getName()
----忙其他任务了);log.debug(futureTask.get(2,
{log.debug(Thread.currentThread().getName()
t1);t1.start();log.debug(Thread.currentThread().getName()
{log.debug(futureTask.get());break;}
{TimeUnit.MILLISECONDS.sleep(500);}
RuntimeException(e);}log.debug(正在处理中...);}}}
资源而且也不见得能及时地得到计算结果。
如果想要异步获取结果通常都会以轮询的方式去获取结果尽量不要阻塞。
对于结果的获取不是很友好只能通过阻塞或轮询的方式得到任务的结果。
的。
但是对于回调通知通过轮询的方式去判断任务是否完成这样非常占用
将多个异步任务的计算结果组合起来后一个异步任务的计算结果需要前一个异步任务的值。
将这两个或多个异步计算合成一个一步计算这几个一步计算相互独立同时后面这个又依赖前一个处理的结果。
对计算速度选最快当Future集合中某个任务最快结束时返回结果返回第一名处理
对于简单的业务场景使用Future完全OK,但想完成上述一些复杂的任务使用Future之前提供的那点API就囊中羞涩处理起来不够优雅这时候还是让CompletableFuture以声明式的方式优雅的处理这些需求。
Future能干的CompletableFuture都能干。
CompletableFuture。
CompletableFuture
提供了一种观察者模式类似的机制可以让人物执行完成后通知监听的一方。
代表异步计算过程中的某一个阶段一个阶段完成以后可能会触发另外一个阶段一个阶段的计算执行可以是一个
Systemm.out.print(x)).thenRun(()
System.out.println())一个阶段的执行可能是被单个阶段的完成触发也可能是由多个阶段一起触发。
的扩展功能可以帮助我们简化异步编程的复杂性并且提供了函数式编程的能力可以通过回调的方式处理计算结果也提供了转换和组合
Future也有可能代表一个完成阶段CompletionStage它支持在计算完成以后触发一些函数或执行某些动作。
它实现了
asyncRunStage(screenExecutor(executor),
asyncSupplyStage(screenExecutor(executor),
没有指定Executor的方法直接使用默认的ForkJoinPool.commonPool()作为它的线程池执行异步代码。
如果指定线程池则使用我们自定义的或者特别指定的线程池执行异步代码。
{log.debug(Thread.currentThread().getName()
RuntimeException(e);}log.debug(--------task
over);});System.out.println(future.get());}
Executors.newFixedThreadPool(3);CompletableFutureVoid
{log.debug(Thread.currentThread().getName()
RuntimeException(e);}log.debug(--------task
executorService);System.out.println(future.get());executorService.shutdown();}
CompletableFuture.supplyAsync(()
{log.debug(Thread.currentThread().getName()
RuntimeException(e);}log.debug(--------task
supplyAsync;});log.debug(future.get());}
Executors.newFixedThreadPool(3);CompletableFutureString
CompletableFuture.supplyAsync(()
{log.debug(Thread.currentThread().getName()
RuntimeException(e);}log.debug(--------task
executorService);log.debug(future.get());executorService.shutdown();}
的功能增强版减少阻塞和轮询可以传入回调对象当异步任务完成或者发生异常时自动调用回调对象的回调方法。
{CompletableFuture.supplyAsync(()
{System.out.println(Thread.currentThread().getName()
ThreadLocalRandom.current().nextInt(10);try
RuntimeException(e);}System.out.println(----1秒钟后出结果
{System.out.println(----计算完成更新系统UpdateValue
{e.printStackTrace();System.out.println(异常情况
null;});System.out.println(Thread.currentThread().getName()
线程池中的线程类似于守护线程由于主线程执行速度过快先执行结束导致
Executors.newFixedThreadPool(3);CompletableFuture.supplyAsync(()
{System.out.println(Thread.currentThread().getName()
ThreadLocalRandom.current().nextInt(10);try
RuntimeException(e);}System.out.println(----1秒钟后出结果
{System.out.println(----计算完成更新系统UpdateValue:
{e.printStackTrace();System.out.println(异常情况
null;});System.out.println(Thread.currentThread().getName()
接口可以接收两个参数其中第一个参数为上一步中产生的结果第二个参数为上一步代码执行过程中产生的异常。
exceptionally(FunctionThrowable,
Executors.newFixedThreadPool(3);CompletableFuture.supplyAsync(()
{System.out.println(Thread.currentThread().getName()
ThreadLocalRandom.current().nextInt(10);try
RuntimeException(e);}System.out.println(----1秒钟后出结果
{System.out.println(----计算完成更新系统UpdateValue:
{e.printStackTrace();System.out.println(异常情况
null;});System.out.println(Thread.currentThread().getName()
异步任务结束时会自动回调某个对象的方法主线程设置好回调后不再关心异步任务的执行异步任务之间可以顺序执行异步任务出错时会自动回调某个对象的方法
函数式接口名称方法名称参数返回值Runnablerun无参数无返回值Functionapply1
个参数无返回值Supplierget无参数有返回值BiConsumeraccept2
同一款产品同时搜索出同款产品在各大电商平台的售价同一款产品同时搜索出本产品在同一个电商平台下各个入驻卖家售价是多少
出来结果希望是同款产品的在不同地方的价格清单列表返回一个List
%.2f,netMall.getNetMallName(),netMall.calPrice(productName))).collect(Collectors.toList());}public
getPriceByCompletableFuture(ListNetMall
-CompletableFuture.supplyAsync(()
%.2f,netMall.getNetMallName(),netMall.calPrice(productName)))).collect(Collectors.toList()).stream().map(s
s.join()).collect(Collectors.toList());}public
getPriceByCompletableFuture(list,
{System.out.println(element);}long
System.currentTimeMillis();System.out.println(----costTime:
ThreadLocalRandom.current().nextDouble()
计算完成就返回正常值否则返回备胎值传入的参数立即获取结果不阻塞
CompletableFuture.supplyAsync(()
RuntimeException(e);}System.out.println(future.getNow(xxx));System.out.println(future.complete(completeValue)
计算结果存在依赖关系两个线程串行化由于存在依赖关系当前步骤出错不再执行下一步当前步骤有异常的话就停止运行。
Executors.newFixedThreadPool(3);CompletableFuture.supplyAsync(()
RuntimeException(e);}System.out.println(111);return
{System.out.println(222);return
{System.out.println(333);return
{e.printStackTrace();;System.out.println(e.getMessage());return
null;});System.out.println(Thread.currentThread().getName()
Executors.newFixedThreadPool(3);CompletableFuture.supplyAsync(()
RuntimeException(e);}System.out.println(111);return
0;System.out.println(222);return
{System.out.println(333);return
{e.printStackTrace();;System.out.println(e.getMessage());return
null;});System.out.println(Thread.currentThread().getName()
计算结果存在依赖关系两个线程串行化有异常也可以继续向下运行根据带的异常参数可以进一步处理
Executors.newFixedThreadPool(3);CompletableFuture.supplyAsync(()
RuntimeException(e);}System.out.println(111);return
0;System.out.println(222);return
{System.out.println(333);return
{e.printStackTrace();;System.out.println(e.getMessage());return
null;});System.out.println(Thread.currentThread().getName()
{CompletableFuture.supplyAsync(()
{System.out.println(CompletableFuture.supplyAsync(()
{}).join());System.out.println(--------------------------------);System.out.println(CompletableFuture.supplyAsync(()
{System.out.println(r);}).join());System.out.println(--------------------------------);System.out.println(CompletableFuture.supplyAsync(()
ForkJoinPool传入了一个自定义线程池如果执行第一个任务的时候传入了一个自定义线程池
方法执行第二个任务时则第二个任务和第一个任务是共用同一个线程池调用
执行第二个任务时则第一个人任务使用的是你自己传入的线程池第二个任务使用的是
CompletableFuture.supplyAsync(()
{TimeUnit.MILLISECONDS.sleep(20);}
RuntimeException(e);}System.out.println(1号任务\t
Thread.currentThread().getName());return
{TimeUnit.MILLISECONDS.sleep(20);}
RuntimeException(e);}System.out.println(2号任务\t
Thread.currentThread().getName());}).thenRun(()
{TimeUnit.MILLISECONDS.sleep(20);}
RuntimeException(e);}System.out.println(3号任务\t
Thread.currentThread().getName());}).thenRun(()
{TimeUnit.MILLISECONDS.sleep(20);}
RuntimeException(e);}System.out.println(4号任务\t
Thread.currentThread().getName());});System.out.println(future.get(2L,
Executors.newFixedThreadPool(3);CompletableFutureVoid
CompletableFuture.supplyAsync(()
{TimeUnit.MILLISECONDS.sleep(20);}
RuntimeException(e);}System.out.println(1号任务\t
Thread.currentThread().getName());return
{TimeUnit.MILLISECONDS.sleep(20);}
RuntimeException(e);}System.out.println(2号任务\t
Thread.currentThread().getName());}).thenRun(()
{TimeUnit.MILLISECONDS.sleep(20);}
RuntimeException(e);}System.out.println(3号任务\t
Thread.currentThread().getName());}).thenRun(()
{TimeUnit.MILLISECONDS.sleep(20);}
RuntimeException(e);}System.out.println(4号任务\t
Thread.currentThread().getName());});System.out.println(future.get(2L,
TimeUnit.SECONDS));pool.shutdown();}
Executors.newFixedThreadPool(3);CompletableFutureVoid
CompletableFuture.supplyAsync(()
{TimeUnit.MILLISECONDS.sleep(20);}
RuntimeException(e);}System.out.println(1号任务\t
Thread.currentThread().getName());return
{TimeUnit.MILLISECONDS.sleep(20);}
RuntimeException(e);}System.out.println(2号任务\t
Thread.currentThread().getName());}).thenRunAsync(()
{TimeUnit.MILLISECONDS.sleep(20);}
RuntimeException(e);}System.out.println(3号任务\t
Thread.currentThread().getName());}).thenRunAsync(()
{TimeUnit.MILLISECONDS.sleep(20);}
RuntimeException(e);}System.out.println(4号任务\t
Thread.currentThread().getName());});System.out.println(future.get(2L,
TimeUnit.SECONDS));pool.shutdown();}
CompletableFuture.supplyAsync(()
planA;});CompletableFutureString
CompletableFuture.supplyAsync(()
planB;});CompletableFutureString
winer;});System.out.println(Thread.currentThread().getName()
CompletableFuture.supplyAsync(()
{System.out.println(Thread.currentThread().getName()
CompletableFuture.supplyAsync(()
{System.out.println(Thread.currentThread().getName()
{System.out.println(-----开始两个结果的合并);return
y;});System.out.println(combine.join());}
作为专业的SEO优化服务提供商,我们致力于通过科学、系统的搜索引擎优化策略,帮助企业在百度、Google等搜索引擎中获得更高的排名和流量。我们的服务涵盖网站结构优化、内容优化、技术SEO和链接建设等多个维度。
| 服务项目 | 基础套餐 | 标准套餐 | 高级定制 |
|---|---|---|---|
| 关键词优化数量 | 10-20个核心词 | 30-50个核心词+长尾词 | 80-150个全方位覆盖 |
| 内容优化 | 基础页面优化 | 全站内容优化+每月5篇原创 | 个性化内容策略+每月15篇原创 |
| 技术SEO | 基本技术检查 | 全面技术优化+移动适配 | 深度技术重构+性能优化 |
| 外链建设 | 每月5-10条 | 每月20-30条高质量外链 | 每月50+条多渠道外链 |
| 数据报告 | 月度基础报告 | 双周详细报告+分析 | 每周深度报告+策略调整 |
| 效果保障 | 3-6个月见效 | 2-4个月见效 | 1-3个月快速见效 |
我们的SEO优化服务遵循科学严谨的流程,确保每一步都基于数据分析和行业最佳实践:
全面检测网站技术问题、内容质量、竞争对手情况,制定个性化优化方案。
基于用户搜索意图和商业目标,制定全面的关键词矩阵和布局策略。
解决网站技术问题,优化网站结构,提升页面速度和移动端体验。
创作高质量原创内容,优化现有页面,建立内容更新机制。
获取高质量外部链接,建立品牌在线影响力,提升网站权威度。
持续监控排名、流量和转化数据,根据效果调整优化策略。
基于我们服务的客户数据统计,平均优化效果如下:
我们坚信,真正的SEO优化不仅仅是追求排名,而是通过提供优质内容、优化用户体验、建立网站权威,最终实现可持续的业务增长。我们的目标是与客户建立长期合作关系,共同成长。
Demand feedback