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

查询网站是否做301廊坊网站推广公司

查询网站是否做301,廊坊网站推广公司,小米网站开发语言,西双版纳傣族自治州勐海县控制三个线程按顺序交替输出数字123123… synchronized#xff08;配合专用锁对象#xff09; 通过共享锁和 volatile 变量控制执行顺序#xff0c;每个线程按指定顺序打印指定内容#xff0c;确保输出序列如 “123123…”。使用 synchronized 和 wait/notifyAll 实现线程间…控制三个线程按顺序交替输出数字123123… synchronized配合专用锁对象 通过共享锁和 volatile 变量控制执行顺序每个线程按指定顺序打印指定内容确保输出序列如 “123123…”。使用 synchronized 和 wait/notifyAll 实现线程间协作。 /*** AlternateOutput类用于实现三个线程交替输出数字的功能* 通过使用synchronized关键字和wait/notify机制确保三个线程按照1-2-3的顺序循环输出*/ public class AlternateOutput {/*** 用于线程同步的锁对象*/private static final Object lock new Object();/*** 当前应该执行的线程编号*/private static int currentThreadNum 1;/*** 每个线程需要执行的循环次数*/private static final int MAX_LOOP 10;/*** 线程执行的任务方法负责按照指定顺序输出内容* param curThreadNum 当前线程的编号用于判断是否轮到当前线程执行* param nextThreadNum 下一个应该执行的线程编号执行完当前任务后更新此值* param output 当前线程需要输出的内容*/private static void runTask(int curThreadNum, int nextThreadNum, String output) {// 循环执行指定次数的任务for (int i 0; i AlternateOutput.MAX_LOOP; i) {// 使用synchronized块确保线程安全synchronized (lock) {// 等待直到轮到当前线程执行while (AlternateOutput.currentThreadNum ! curThreadNum) {try {lock.wait();} catch (InterruptedException e) {Thread.currentThread().interrupt();return;}}// 输出当前线程的内容System.out.print(output);// 更新下一个应该执行的线程编号currentThreadNum nextThreadNum;// 唤醒所有等待的线程lock.notifyAll();}}}/*** 程序入口点创建并启动三个线程实现交替输出* param args 命令行参数*/public static void main(String[] args) {// 创建第一个线程负责输出1Thread t1 new Thread(() - {runTask(1, 2, 1);});t1.start();// 创建第二个线程负责输出2Thread t2 new Thread(() - {runTask(2, 3, 2);});t2.start();// 创建第三个线程负责输出3Thread t3 new Thread(() - {runTask(3, 1, 3);});t3.start();// 等待所有线程执行完成try {t1.join();t2.join();t3.join();} catch (InterruptedException e) {Thread.currentThread().interrupt();}} }ReentrantLock和Condition 通过ReentrantLock和Condition控制执行顺序确保按1→2→3循环打印10次。每个线程等待其编号轮次执行打印后唤醒下一个线程。 import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock;public class PrintNumber {public static final int MAX_LOOP 10;public static int currentNum 1;public static final ReentrantLock lock new ReentrantLock();public static final Condition c1 lock.newCondition();public static final Condition c2 lock.newCondition();public static final Condition c3 lock.newCondition();public static void main(String[] args) {Thread t1 new Thread(new PrintNumberTask(1));Thread t2 new Thread(new PrintNumberTask(2));Thread t3 new Thread(new PrintNumberTask(3));t1.start();t2.start();t3.start();} }import java.util.concurrent.locks.Condition;public class PrintNumberTask implements Runnable {private int number;public PrintNumberTask(int num) {this.number num;}private void printAndSwith(String output, int nextNum, Condition awaitCondition, Condition signalCondition)throws InterruptedException {while (PrintNumber.currentNum ! this.number) {awaitCondition.await();}System.out.print(output);PrintNumber.currentNum nextNum;signalCondition.signal();}Overridepublic void run() {for (int i 0; i PrintNumber.MAX_LOOP; i) {PrintNumber.lock.lock();try {switch (number) {case 1:printAndSwith(1, 2, PrintNumber.c1, PrintNumber.c2);break;case 2:printAndSwith(2, 3, PrintNumber.c2, PrintNumber.c3);break;case 3:printAndSwith(3, 1, PrintNumber.c3, PrintNumber.c1);break;default:break;}} catch (Exception e) {e.printStackTrace();} finally {PrintNumber.lock.unlock();}}} }
http://www.zqtcl.cn/news/701010/

相关文章:

  • 龙华营销型网站制作企业网站模板源代码下载
  • 山东城乡建设厅网站哪有做网站公司
  • 建设网站是否等于开展网络营销用wordPress搭建图片库
  • 泗阳做网站的外贸公司网站搭建
  • 做汽车保养的网站上商业招商网站
  • 如何进网站帝国cms调用网站名称
  • 瑞金网站建设推广合肥瑶海区地图
  • 静态网站建设国内免费域名
  • 网站建设设计公司电子商务网站开发与管理
  • 手机网站制作设计做国际网站有什么需要注意的
  • 机构网站源码如何分析一个网站
  • 免费营销软件网站网站建设与规划实训总结
  • 网站深度功能建筑人才网市场
  • 学校网站建设的意义和应用服务平台管理系统
  • 网站内容规划要包括什么内容wordpress5.2 php版本
  • 山西建设部网站超值的镇江网站建设
  • 做淘宝要网站网站推广外链怎么做
  • 深圳做网站推广哪家好自建网站优缺点
  • 网站建设询价函什么网站可以做会计题目
  • 电脑网站视频怎么下载珠海免费网站制作
  • wordpress menu icon咸阳seo
  • php制作网站网站开发与客户沟通
  • 百度网站建设平台微盟微商城官网
  • 三明网站seo上海中学分数线
  • 青岛谷歌网站建设网站建站公司排名
  • 成都旅游网站建设规划windows优化大师官方
  • 福永网站建设公司哪家好财务公司承兑汇票
  • 青岛快速建站模板制作公司网页什么价位
  • 网站建设公司的经营范围wordpress设置文本编辑器
  • 做网站用微软雅黑侵权吗wordpress 同类文章