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

php连接wordpress天津seo诊断

php连接wordpress,天津seo诊断,十大免费壁纸软件,网站续费有什么作用文章目录 1、串行化执行2、并行化测试(多线程环境)3、任务的执行是异步的,但主程序的继续执行是同步的 可以将多个任务编排为并行和串行化执行。 也可以处理编排的多个任务的异常,也可以返回兜底数据。 1、串行化执行 顺序执行、…

文章目录

  • 1、串行化执行
  • 2、并行化测试(多线程环境)
  • 3、任务的执行是异步的,但主程序的继续执行是同步的

可以将多个任务编排为并行和串行化执行。
也可以处理编排的多个任务的异常,也可以返回兜底数据。

1、串行化执行

顺序执行、同步执行
按顺序同步执行
导入 StopWatch 类,这是 Spring 框架提供的一个工具类,用于测量任务的执行时间。

package com.atguigu.structure;import org.springframework.util.StopWatch;public class Demo {public static void main(String[] args) throws InterruptedException {StopWatch stopWatch = new StopWatch();// 为每个任务分别计时stopWatch.start("a任务");a();stopWatch.stop();stopWatch.start("b任务");b();stopWatch.stop();stopWatch.start("c任务");c();stopWatch.stop();// 此时,StopWatch 已经自动停止了总任务的计时(因为最后一个任务也已经停止)// 但如果您想显式地停止总任务(尽管在这个例子中它是多余的),您需要确保没有正在运行的任务// 打印结果System.out.println(stopWatch.prettyPrint());}public static void a() throws InterruptedException {Thread.sleep(3000);System.out.println(Thread.currentThread().getName() + " a任务执行完毕-" + System.currentTimeMillis());}public static void b() throws InterruptedException {Thread.sleep(2000);System.out.println(Thread.currentThread().getName() + " b任务执行完毕-" + System.currentTimeMillis());}public static void c() throws InterruptedException {Thread.sleep(1000);System.out.println(Thread.currentThread().getName() + " c任务执行完毕-" + System.currentTimeMillis());}
}

由于 a 任务最先执行,它的开始时间是 0 毫秒,结束时间是 3000 毫秒。b 任务在 a 任务结束后立即开始,因此它的开始时间是 3000 毫秒,结束时间是 5000 毫秒。c 任务在 b 任务结束后立即开始,因此它的开始时间是 5000 毫秒,结束时间是 6000 毫秒。

main a任务执行完毕-1727613404687
main b任务执行完毕-1727613406705
main c任务执行完毕-1727613407711
StopWatch '': 6.0325798 seconds
----------------------------------------
Seconds       %       Task name
----------------------------------------
3.0193421     50%     a任务
2.007708      33%     b任务
1.0055297     17%     c任务

2、并行化测试(多线程环境)

多个并发任务
三个任务将并行执行

注意:这里主线程不会等待任务线程完成,因此程序可能会立即退出。
如果需要等待所有任务完成,可以使用CountDownLatch或其他同步机制。

package com.atguigu.structure;import org.springframework.util.StopWatch;public class Demo {public static void main(String[] args) throws InterruptedException {StopWatch stopWatch = new StopWatch();// 为每个任务分别计时stopWatch.start("a任务");new Thread(()->{a();}).start();stopWatch.stop();stopWatch.start("b任务");new Thread(()->{b();}).start();stopWatch.stop();stopWatch.start("c任务");new Thread(()->{c();}).start();stopWatch.stop();// 打印结果System.out.println(stopWatch.prettyPrint());}public static void a(){try {Thread.sleep(3000);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(Thread.currentThread().getName() + " a任务执行完毕-" + System.currentTimeMillis());}public static void b(){try {Thread.sleep(2000);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(Thread.currentThread().getName() + " b任务执行完毕-" + System.currentTimeMillis());}public static void c(){try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(Thread.currentThread().getName() + " c任务执行完毕-" + System.currentTimeMillis());}
}
StopWatch '': 0.0022191 seconds
----------------------------------------
Seconds       %       Task name
----------------------------------------
0.0011501     52%     a任务
0.0005202     23%     b任务
0.0005488     25%     c任务Thread-2 c任务执行完毕-1727615633886
Thread-1 b任务执行完毕-1727615634883
Thread-0 a任务执行完毕-1727615635882

StopWatch 的 start() 和 stop() 方法调用是在主线程中顺序执行的,但是实际的任务(a(), b(), c())是在不同的线程中异步执行的。这会导致 StopWatch 的 stop() 方法在相应的任务线程实际完成之前就被调用了,因此 StopWatch 记录的时间将远小于任务实际执行的时间。

3、任务的执行是异步的,但主程序的继续执行是同步的

使用f1.get(), f2.get(), f3.get()确保主线程等待这些任务完成。

  • 即每个任务都使用了独立的 FutureTask 和线程实例。但是,有一点需要注意:在 main 线程中调用 f1.get(), f2.get(), 和 f3.get() 会阻塞 main 线程,直到相应的 FutureTask 完成。这实际上意味着 main 线程会等待每个任务完成后再继续执行下一个任务,这可能会使得并行执行的优势变得不那么明显,因为任务实际上是顺序执行的(尽管它们在不同的线程中运行)。
  • 因此,从主线程的角度来看,这些任务并不是“真正”的异步执行,因为主线程在等待每个任务完成。然而,从操作系统或 JVM 的角度来看,这些任务确实是在不同的线程中并行执行的(如果系统资源允许的话)。
  • 从全局角度来看,每个任务虽然是异步启动的,但它们依次等待完成,这使得整个程序看起来是按顺序执行的。
  • 主程序通过调用 f1.get(), f2.get(), 和 f3.get() 等待每个任务完成。这意味着主程序会阻塞,直到所有任务都执行完毕。
  • 这种方式确保了任务的执行时间能够被准确地测量,但也意味着主程序不会在所有任务完成之前继续执行其他操作。
  • 由于您还希望使用 StopWatch 来测量每个任务的执行时间,您需要在某个地方等待这些任务完成,所以通过调用 f1.get(), f2.get(), 和 f3.get() 在主线程中阻塞了这些 FutureTask 的执行结果。

总结来说,任务的执行是异步的,但主程序的继续执行是同步的,因为它等待所有异步任务完成。

package com.atguigu.structure;import org.springframework.util.StopWatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;public class Demo {public static void main(String[] args) throws InterruptedException, ExecutionException {StopWatch stopWatch = new StopWatch();// 为每个任务分别计时stopWatch.start("a任务");FutureTask f1 = new FutureTask<>(() -> {a();return null;});new Thread(f1).start();f1.get();stopWatch.stop();stopWatch.start("b任务");FutureTask f2 = new FutureTask<>(() -> {b();return null;});new Thread(f2).start();f2.get();stopWatch.stop();stopWatch.start("c任务");FutureTask f3 = new FutureTask<>(() -> {c();return null;});new Thread(f3).start();f3.get();stopWatch.stop();// 打印结果System.out.println(stopWatch.prettyPrint());}public static void a(){try {Thread.sleep(3000);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(Thread.currentThread().getName() + " a任务执行完毕-" + System.currentTimeMillis());}public static void b(){try {Thread.sleep(2000);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(Thread.currentThread().getName() + " b任务执行完毕-" + System.currentTimeMillis());}public static void c(){try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(Thread.currentThread().getName() + " c任务执行完毕-" + System.currentTimeMillis());}
}
Thread-0 a任务执行完毕-1727619923910
Thread-1 b任务执行完毕-1727619925934
Thread-2 c任务执行完毕-1727619926943
StopWatch '': 6.0475203 seconds
----------------------------------------
Seconds       %       Task name
----------------------------------------
3.0258235     50%     a任务
2.0129727     33%     b任务
1.0087241     17%     c任务

在这里插入图片描述

http://www.yidumall.com/news/55248.html

相关文章:

  • 临朐网站做的好的免费站推广网站在线
  • 比较好的做网站的公司app 推广
  • 广州做手机网站咨询线上销售如何找到精准客户
  • 做本地网站应该选什么内容seo技术建站
  • 做网站需要字体切换自媒体营销代理
  • 网页设计毕业论文目录济南搜索引擎优化网站
  • 做p2p网站 预算多少快速优化关键词排名
  • 公安局网站不备案吗百度关键字搜索排名
  • wordpress 文字底色专业seo网络营销公司
  • 四川网站建设广元分公司北京网站推广排名服务
  • 软件网网站优化排名推荐
  • wordpress扫码付费可见插件seo算法优化
  • 西丽建设网站深圳最好的外贸seo培训
  • 广丰做网站公司百度网页游戏排行榜
  • 020网站开发多少钱nba最新交易信息
  • 旅游前 做攻略有什么网站好用成都网站排名优化公司
  • 做超市海报的网站郑州技术支持seo
  • 手机做网站怎么做推广营销软件app
  • 给卖假性药的做网站一般要判多久关键词代发排名
  • 做网站很赚钱免费网站安全检测
  • php网络公司网站源码宁波正规优化seo公司
  • 成都网站建设v淘宝客推广平台
  • 国内哪家公司做网站最好网络优化报告
  • 网站建设選宙斯王免费网络营销平台
  • 天津个人做网站做网站需要准备什么
  • 有什么值得做的网站关键词制作软件
  • 滨州做网站的公司湖南网站seo推广
  • 罗泾网站建设网页设计与制作代码
  • 帮人做网站收费合法吗宁波网站推广网站优化
  • 网站开发有限公司seo网站推广助理招聘