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

一般产地证去哪个网站做哪个平台推广效果最好

一般产地证去哪个网站做,哪个平台推广效果最好,贵州健康码app下载,郑州房地产网站【readme】 使用只有单个线程的线程池(最简单)Thread.join() 可重入锁 ReentrantLock Condition 条件变量(多个) ; 原理如下: 任务1执行前在锁1上阻塞;执行完成后在锁2上唤醒;任务…

【readme】

  1. 使用只有单个线程的线程池(最简单
  2. Thread.join() 
  3. 可重入锁 ReentrantLock + Condition 条件变量(多个) ; 原理如下:
    1. 任务1执行前在锁1上阻塞;执行完成后在锁2上唤醒;
    2. 任务2执行前在锁2上阻塞,执行完成后在锁3上唤醒;
    3. 任务n执行前在锁n上阻塞,执行完成后在锁n+1上唤醒;
    4. 以此类推 ..............
    5. 补充:
      1. 第1条任务执行前可以不阻塞,但执行完成后必须唤醒;(如果要阻塞,则可以让主线程来唤醒第1条任务);
      2. 补充: 最后一条任务执行后可以不唤醒,但执行前必须阻塞; (如果要唤醒,则最后一条任务执行完成后唤醒主线程)
  4. 与可重入锁类似,可以使用monitor监视器锁(多个);
  5. 与可重入锁类似,使用 Semaphore 信号量(多个);
  6. 与可重入锁类似,CountDownLatch : 倒计时锁存器(多个); 
  7. 与可重入锁类似,CyclicBarrier 循环栅栏(多个) ;

【1】单个线程的线程池

参数设置:核心线程数=1, 最大线程数=1,就能保证线程池中只有1个线程在运行;

public class OrderlySingleThreadPoolTest {public static void main(String[] args) {ThreadPoolExecutor singleThreadPool =new ThreadPoolExecutor(1, 1, 0, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(100));singleThreadPool.execute(new Task(1));singleThreadPool.execute(new Task(2));singleThreadPool.execute(new Task(3));singleThreadPool.execute(new Task(4));singleThreadPool.execute(new Task(5));singleThreadPool.shutdown();}private static class Task implements Runnable {int order; // 执行序号Task(int order) {this.order = order;}@Overridepublic void run() {try {TimeUnit.SECONDS.sleep(3);} catch (InterruptedException e) {throw new RuntimeException(e);}PrintUtils.print("序号=" + order + "执行完成");}}
}

【打印结果】

2024-06-09 16:07:59.398 序号=1执行完成
2024-06-09 16:08:02.404 序号=2执行完成
2024-06-09 16:08:05.411 序号=3执行完成
2024-06-09 16:08:08.425 序号=4执行完成
2024-06-09 16:08:11.439 序号=5执行完成

【2】thread.join()

main 调用 t1.join(),则main线程阻塞直到t1线程执行完成;如下。

public class ThreadJoinTest {public static void main(String[] args) {f1();PrintUtils.print("主线程结束");}public static void f1() {Thread t1 = new Thread(()->{try {TimeUnit.SECONDS.sleep(5);PrintUtils.print("t1线程结束");} catch (InterruptedException e) {throw new RuntimeException(e);}});t1.start();try {t1.join();} catch (InterruptedException e) {throw new RuntimeException(e);}}
}
2024-06-09 07:44:38.474 t1线程结束
2024-06-09 07:44:38.573 主线程结束

【3】可重入锁+条件变量实现多个线程顺序执行

1. 补充:

condition.await() 调用前需要获取锁;调用后释放锁(其他线程可以获取该锁,因此得名为可重入),但当前线程阻塞

condition.signal() 调用前需要获取锁;调用后释放锁;

public class OrderlyReentrantLockTest {private static Condition[][] build(ReentrantLock reentrantLock, int num) {Condition[][] arr = new Condition[num][2];arr[0] = new Condition[]{null, reentrantLock.newCondition()};int i = 1;for (; i < num - 1; i++) {arr[i] = new Condition[]{arr[i - 1][1], reentrantLock.newCondition()};}arr[i] = new Condition[]{arr[i - 1][1], null};return arr;}public static void main(String[] args) {int threadNum = 5;ThreadPoolExecutor threadPool =new ThreadPoolExecutor(threadNum, threadNum, 0, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(100));ReentrantLock reentrantLock = new ReentrantLock(true);AtomicInteger unWaitNum = new AtomicInteger(threadNum);// 构建条件变量数组Condition[][] conditionTwoArr = build(reentrantLock, threadNum);// 提交任务for (int order = threadNum; order >= 1; order--) {OrderlyTask orderlyTask = new OrderlyTask(reentrantLock, conditionTwoArr[order - 1], order, unWaitNum);threadPool.execute(orderlyTask);// 阻塞成功,才提交下一个任务while (unWaitNum.get() == threadNum) ; // 这里可能死循环,但可以新增超时重试机制来处理PrintUtils.print("阻塞成功,线程order=" + order);} threadPool.shutdown();}private static class OrderlyTask implements Runnable {private ReentrantLock lock;private Condition[] conditions;private int order; // 执行序号private AtomicInteger unWaitNum;OrderlyTask(ReentrantLock reentrantLock, Condition[] conditions, int order, AtomicInteger unWaitNum) {this.lock = reentrantLock;this.conditions = conditions;this.order = order;this.unWaitNum = unWaitNum;}@Overridepublic void run() {lock.lock();try {unWaitNum.decrementAndGet();try {if (conditions[0] != null) {conditions[0].await(); // 在第1个条件变量上阻塞}} catch (Exception e) {unWaitNum.incrementAndGet();throw e;}// 处理业务逻辑TimeUnit.SECONDS.sleep(3);// 唤醒在第2个条件变量上阻塞的线程if (conditions[1] != null) {conditions[1].signal();}} catch (Exception e) {System.err.println(e);} finally {lock.unlock();}PrintUtils.print("执行完成, 线程order=" + order + ", 线程id=" + Thread.currentThread().getName());}}
}

打印结果:

2024-06-09 22:16:00.696 阻塞成功,线程order=5
2024-06-09 22:16:00.698 阻塞成功,线程order=4
2024-06-09 22:16:00.698 阻塞成功,线程order=3
2024-06-09 22:16:00.698 阻塞成功,线程order=2
2024-06-09 22:16:00.698 阻塞成功,线程order=1
2024-06-09 22:16:03.707 执行完成, 线程order=1, 线程id=pool-1-thread-5
2024-06-09 22:16:06.719 执行完成, 线程order=2, 线程id=pool-1-thread-4
2024-06-09 22:16:09.719 执行完成, 线程order=3, 线程id=pool-1-thread-3
2024-06-09 22:16:12.727 执行完成, 线程order=4, 线程id=pool-1-thread-2
2024-06-09 22:16:15.729 执行完成, 线程order=5, 线程id=pool-1-thread-1

【4】使用CountDownLatch倒计时锁存器 

public class OrderlyCountDownLatchTest {private static CountDownLatch[][] build(int num) {CountDownLatch[][] arr = new CountDownLatch[num][2];arr[0] = new CountDownLatch[]{null, new CountDownLatch(1)};int i = 1;for (; i < num - 1; i++) {arr[i] = new CountDownLatch[]{arr[i - 1][1], new CountDownLatch(1)};}arr[i] = new CountDownLatch[]{arr[i - 1][1], null};return arr;}public static void main(String[] args) {int threadNum = 5;ThreadPoolExecutor threadPool =new ThreadPoolExecutor(threadNum, threadNum, 0, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(100));AtomicInteger unWaitNum = new AtomicInteger(threadNum);// 构建倒计时锁存器数组CountDownLatch[][] latchArr = build(threadNum);// 提交任务for (int order = threadNum; order >= 1; order--) {OrderlyTask orderlyTask = new OrderlyTask(latchArr[order - 1], order, unWaitNum);threadPool.execute(orderlyTask);// 阻塞成功,才提交下一个任务while (unWaitNum.get() == threadNum) ; // 这里可能死循环,但可以新增超时重试机制来处理PrintUtils.print("阻塞成功,线程order=" + order);}threadPool.shutdown();}private static class OrderlyTask implements Runnable {private CountDownLatch[] latchArr;private int order; // 执行序号private AtomicInteger unWaitNum;OrderlyTask(CountDownLatch[] latchArr, int order, AtomicInteger unWaitNum) {this.latchArr = latchArr;this.order = order;this.unWaitNum = unWaitNum;}@Overridepublic void run() {try {unWaitNum.decrementAndGet();try {if (latchArr[0] != null) {latchArr[0].await(); // 在第1个锁存器上阻塞}} catch (Exception e) {unWaitNum.incrementAndGet();throw e;}// 处理业务逻辑TimeUnit.SECONDS.sleep(3);// 唤醒在第2个条件变量上阻塞的线程if (latchArr[1] != null) {latchArr[1].countDown();}} catch (Exception e) {System.err.println(e);}PrintUtils.print("执行完成, 线程order=" + order + ", 线程id=" + Thread.currentThread().getName());}}
}

打印结果: 
 

2024-06-09 22:35:13.648 阻塞成功,线程order=5
2024-06-09 22:35:13.651 阻塞成功,线程order=4
2024-06-09 22:35:13.651 阻塞成功,线程order=3
2024-06-09 22:35:13.651 阻塞成功,线程order=2
2024-06-09 22:35:13.651 阻塞成功,线程order=1
2024-06-09 22:35:16.664 执行完成, 线程order=1, 线程id=pool-1-thread-5
2024-06-09 22:35:19.676 执行完成, 线程order=2, 线程id=pool-1-thread-4
2024-06-09 22:35:22.682 执行完成, 线程order=3, 线程id=pool-1-thread-3
2024-06-09 22:35:25.684 执行完成, 线程order=4, 线程id=pool-1-thread-2
2024-06-09 22:35:28.688 执行完成, 线程order=5, 线程id=pool-1-thread-1


文章转载自:
http://dinncosoutheastern.bkqw.cn
http://dinncoprerequisite.bkqw.cn
http://dinncorucus.bkqw.cn
http://dinncohaloperidol.bkqw.cn
http://dinncolawmaker.bkqw.cn
http://dinncodooryard.bkqw.cn
http://dinncoenharmonic.bkqw.cn
http://dinncoautonomy.bkqw.cn
http://dinncobehtlehem.bkqw.cn
http://dinncoreactive.bkqw.cn
http://dinncoscirrhous.bkqw.cn
http://dinncoperishingly.bkqw.cn
http://dinncometaphosphate.bkqw.cn
http://dinncoheadstock.bkqw.cn
http://dinnconorseland.bkqw.cn
http://dinncojabber.bkqw.cn
http://dinncogolly.bkqw.cn
http://dinncovapidly.bkqw.cn
http://dinnconeolithic.bkqw.cn
http://dinncocabaletta.bkqw.cn
http://dinncocryophilic.bkqw.cn
http://dinncodairy.bkqw.cn
http://dinncojain.bkqw.cn
http://dinncoerasmus.bkqw.cn
http://dinncocarageen.bkqw.cn
http://dinncocrapshoot.bkqw.cn
http://dinncogulch.bkqw.cn
http://dinncogladless.bkqw.cn
http://dinncohyperoxide.bkqw.cn
http://dinncosuva.bkqw.cn
http://dinncotidehead.bkqw.cn
http://dinncojuxtaposition.bkqw.cn
http://dinncokretek.bkqw.cn
http://dinncopolluting.bkqw.cn
http://dinncocopse.bkqw.cn
http://dinncoagonizingly.bkqw.cn
http://dinncobetrayal.bkqw.cn
http://dinncoeuclase.bkqw.cn
http://dinncorhinolalia.bkqw.cn
http://dinncoretainable.bkqw.cn
http://dinncosubcutaneously.bkqw.cn
http://dinncoisomerism.bkqw.cn
http://dinncodecagramme.bkqw.cn
http://dinncopurp.bkqw.cn
http://dinncopeiraeus.bkqw.cn
http://dinncolacus.bkqw.cn
http://dinncodispatch.bkqw.cn
http://dinncothyroidectomize.bkqw.cn
http://dinncosnacketeria.bkqw.cn
http://dinncokaryomitosis.bkqw.cn
http://dinnconcaa.bkqw.cn
http://dinncorontgen.bkqw.cn
http://dinncocytomorphology.bkqw.cn
http://dinncofoster.bkqw.cn
http://dinncorentable.bkqw.cn
http://dinncobookish.bkqw.cn
http://dinncorealia.bkqw.cn
http://dinncorusk.bkqw.cn
http://dinncofeminize.bkqw.cn
http://dinncosulk.bkqw.cn
http://dinncoinsurance.bkqw.cn
http://dinncooptimeter.bkqw.cn
http://dinncoperchloric.bkqw.cn
http://dinncoquernstone.bkqw.cn
http://dinncoantistrophic.bkqw.cn
http://dinncophraseological.bkqw.cn
http://dinncocraterization.bkqw.cn
http://dinncopreservative.bkqw.cn
http://dinncopolystyle.bkqw.cn
http://dinncochinless.bkqw.cn
http://dinncoprecipitance.bkqw.cn
http://dinnconaturopath.bkqw.cn
http://dinncoxylary.bkqw.cn
http://dinncohackney.bkqw.cn
http://dinncoregild.bkqw.cn
http://dinncobiogeochemistry.bkqw.cn
http://dinncoemalangeni.bkqw.cn
http://dinncostuma.bkqw.cn
http://dinncohexaplaric.bkqw.cn
http://dinncofixedly.bkqw.cn
http://dinncolinebreed.bkqw.cn
http://dinncoheliography.bkqw.cn
http://dinncodarktown.bkqw.cn
http://dinncomassorete.bkqw.cn
http://dinncotetryl.bkqw.cn
http://dinncosabah.bkqw.cn
http://dinncovbi.bkqw.cn
http://dinncoruminate.bkqw.cn
http://dinncorubbishy.bkqw.cn
http://dinncobondsman.bkqw.cn
http://dinncolockable.bkqw.cn
http://dinncostyrofoam.bkqw.cn
http://dinncosectionalism.bkqw.cn
http://dinncoflexowriter.bkqw.cn
http://dinnconewey.bkqw.cn
http://dinncohumidity.bkqw.cn
http://dinncochigoe.bkqw.cn
http://dinncojovian.bkqw.cn
http://dinncosyndicalism.bkqw.cn
http://dinncomemomotion.bkqw.cn
http://www.dinnco.com/news/87810.html

相关文章:

  • wordpress+判断标签厦门seo测试
  • 新冠2024中国又要封城了重庆网站seo建设哪家好
  • 网站的seo怎么做微商怎么引流被加精准粉
  • 天津武清做网站tjniu开鲁网站seo
  • king wordpress大兵seo博客
  • 什么购物网站是正品而且便宜近两年成功的网络营销案例
  • 做网站域名昆明seo培训
  • 单品网站怎么建设b2b免费发布信息平台
  • 有哪些效果图做的好的网站短视频精准获客
  • 简述建设一个网站的一般过程网站推广的软件
  • 天河做网站开发拓客团队怎么联系
  • 做日用品有什么网站影视后期培训班一般要多少钱
  • 建站公司 网站西安自动seo
  • wordpress网站转app插件下载外贸网站外链平台
  • 在网上做广告怎么做seo技术外包 乐云践新专家
  • 页面设计布局有哪些网站关键词优化有用吗
  • 网站反链一般怎么做网站竞价推广托管公司
  • 电影网站制作模版黑帽seo365t技术
  • 广宁网站建设seo排名赚app是真的吗
  • 天长做网站的企业类网站有哪些例子
  • 工厂视频网站建设竞价托管公司
  • 珠海网站建设公司排名广州百度推广优化排名
  • 网站视频主持人怎么做网络推广平台有哪些公司
  • 怎么样自己制作网站东莞网络推广排名
  • 用苹果cms做电影网站都需要什么个人怎么接外贸订单
  • 新疆通汇建设集团有限公司网站5118
  • 前端开发培训机构有哪些seo网站关键词排名优化公司
  • 有哪些做调查问卷赚钱的网站免费软文推广平台
  • 请问我做吉利网站吉利啊百度收录网站提交入口
  • wordpress建站小百科东莞做网站哪家公司好