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

高阳县做企业网站qq手机版在线登录入口

高阳县做企业网站,qq手机版在线登录入口,注册公司如何提供注册地址,html网站尺寸文章目录 1、如何实现多线程交替打印字母和数字#xff0c;打印效果#xff1a;A1B2C3D4...AutomicBlockingQueueReentrantLockLockSupportSynchronizedWaitNotifyTransferQueueWay 2、实现多个线程顺序打印abc3、实现阻塞队列 1、如何实现多线程交替打印字母和数字#xff… 文章目录 1、如何实现多线程交替打印字母和数字打印效果A1B2C3D4...AutomicBlockingQueueReentrantLockLockSupportSynchronizedWaitNotifyTransferQueueWay 2、实现多个线程顺序打印abc3、实现阻塞队列 1、如何实现多线程交替打印字母和数字打印效果A1B2C3D4… Automic public class AutomicWay {volatile static char num1 A;volatile static int num2 1;static AtomicInteger atomicInteger new AtomicInteger(1);public static void main(String[] args) {new Thread(() - {for (int i 0; i 26; i) {while (atomicInteger.get() ! 1) {}System.out.print(num1);atomicInteger.set(2);}}).start();new Thread(() - {for (int i 0; i 26; i) {while (atomicInteger.get() ! 2) {}System.out.print(num2);atomicInteger.set(1);}}).start();} }BlockingQueue public class BlockingQueueWay {volatile static char num1 A;volatile static int num2 1;public static void main(String[] args) {BlockingQueue queue1 new ArrayBlockingQueue(1);BlockingQueue queue2 new ArrayBlockingQueue(1);new Thread(() - {try {for (int i 0; i 26; i) {System.out.print(num1);queue2.put(到你);queue1.take();}} catch (InterruptedException e) {e.printStackTrace();}}).start();new Thread(() - {try {for (int i 0; i 26; i) {queue2.take();System.out.print(num2);queue1.put(到你);}} catch (InterruptedException e) {e.printStackTrace();}}).start();} }ReentrantLock public class ConditionWay {volatile static char num1 A;volatile static int num2 1;public static void main(String[] args) {ReentrantLock lock new ReentrantLock();Condition conditionA lock.newCondition();Condition conditionB lock.newCondition();new Thread(() - {try {lock.lock();for (int i 0; i 26; i) {System.out.print(num1);conditionB.signal();conditionA.await();}conditionB.signal();} catch (InterruptedException e) {e.printStackTrace();} finally {lock.unlock();}}).start();new Thread(() - {try {lock.lock();for (int i 0; i 26; i) {System.out.print(num2);conditionA.signal();conditionB.await();}conditionA.signal();} catch (InterruptedException e) {e.printStackTrace();} finally {lock.unlock();}}).start();} }LockSupport public class LockSupportWay {static Thread t1,t2 null;volatile static char num1 A;volatile static int num2 1;public static void main(String[] args) {t1 new Thread(()-{for (int i 0; i 26; i) {System.out.print(num1);LockSupport.unpark(t2);LockSupport.park(t1);}});t2 new Thread(()-{for (int i 0; i 26; i) {LockSupport.park(t2);System.out.print(num2);LockSupport.unpark(t1);}});t1.start();t2.start();} }SynchronizedWaitNotify public class SyncWaitNotifyWay {volatile static char num1 A;volatile static int num2 1;public static volatile boolean flag false;public static void main(String[] args) {Object o new Object();new Thread(()-{synchronized (o){flag true;for (int i 0; i 26; i) {System.out.print(num1);try {o.notify();o.wait();} catch (InterruptedException e) {e.printStackTrace();}}o.notify();}}).start();new Thread(()-{synchronized (o){// 只是为了保证执行A的先跑// while (!flag){// try {// o.wait();// } catch (InterruptedException e) {// e.printStackTrace();// }// }for (int i 0; i 26; i) {System.out.print(num2);try {o.notify();o.wait();} catch (InterruptedException e) {e.printStackTrace();}}o.notify();}}).start();} }TransferQueueWay public class TransferQueueWay {volatile static char num1 A;volatile static int num2 1;public static void main(String[] args) {TransferQueue transferQueue new LinkedTransferQueue();new Thread(() - {try {for (int i 0; i 26; i) {System.out.print(transferQueue.take());transferQueue.transfer(num2);}} catch (InterruptedException e) {e.printStackTrace();}}).start();new Thread(() - {try {for (int i 0; i 26; i) {transferQueue.transfer(num1);System.out.print(transferQueue.take());}} catch (InterruptedException e) {e.printStackTrace();}}).start();}}2、实现多个线程顺序打印abc 核心代码 public class PrintABC {ReentrantLock lock new ReentrantLock();Condition conditionA lock.newCondition();Condition conditionB lock.newCondition();Condition conditionC lock.newCondition();private int count;public PrintABC(int count) {this.count count;}volatile int value 0;public void printABC() {new Thread(new ThreadA()).start();new Thread(new ThreadB()).start();new Thread(new ThreadC()).start();}class ThreadA implements Runnable {Overridepublic void run() {lock.lock();try {for (int i 0; i count; i) {while (value % 3 ! 0) {conditionA.await();}System.out.print(A);conditionB.signal();value;}} catch (InterruptedException e) {e.printStackTrace();}}}class ThreadB implements Runnable {Overridepublic void run() {lock.lock();try {for (int i 0; i count; i) {while (value % 3 ! 1) {conditionB.await();}System.out.print(B);conditionC.signal();value;}} catch (InterruptedException e) {e.printStackTrace();}}}class ThreadC implements Runnable {Overridepublic void run() {lock.lock();try {for (int i 0; i count; i) {while (value % 3 ! 2) {conditionC.await();}System.out.print(C);conditionA.signal();value;}} catch (InterruptedException e) {e.printStackTrace();}}} }测试代码 public static void main(String[] args) {PrintABC printABC new PrintABC(10);printABC.printABC(); }// 输出结果ABCABCABCABCABCABCABCABCABCA3、实现阻塞队列 核心代码 public class ProviderConsumerT {private int length;private QueueT queue;private ReentrantLock lock new ReentrantLock();private Condition provideCondition lock.newCondition();private Condition consumeCondition lock.newCondition();public ProviderConsumer(int length) {this.length length;this.queue new LinkedList();}public void provide(T product) {lock.lock();try {while (queue.size() length) { // 不能换成if唤醒后可能条件已经不满足了provideCondition.await();}queue.add(product);consumeCondition.signal();} catch (InterruptedException e) {e.printStackTrace();} finally {lock.unlock();}}public T consume() {lock.lock();try {while (queue.isEmpty()) { // 不能换成if唤醒后可能条件已经不满足了consumeCondition.await();}T product queue.remove();provideCondition.signal();return product;} catch (InterruptedException e) {e.printStackTrace();} finally {lock.unlock();}return null;} }测试代码 public static void main(String[] args) {ProviderConsumerInteger providerConsumer new ProviderConsumer(5);new Thread(() - {for (int i 0; i 10; i) {providerConsumer.provide(1);}}).start();new Thread(() - {for (int i 0; i 10; i) {providerConsumer.provide(2);}}).start();new Thread(() - {for (int i 0; i 100; i) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(providerConsumer.consume());}}).start(); }
http://www.hkea.cn/news/14335986/

相关文章:

  • 汽修网站建设免费溧阳网页设计
  • 图库网站cms网站模板定制
  • 一站式外贸服务平台网站流量统计表格
  • php wordpress 关系南通百度网站快速优化
  • 怎么查询网站有没备案源码如何搭建网站
  • 做啤酒纸箱包装的网站医药外贸是做什么的
  • 什么是自主设计网站常州网站建设书生商友
  • 万网网站开发wordpress搜索框
  • 卡盟网站开发阿里云网站模板
  • 公司网站主页怎么做免费动漫网站
  • 大二学生做网站难吗百度seo营销推广多少钱
  • 网站更新迭代运营公众号还是做网站
  • 网站交互行为做面食专业网站
  • 手机膜 东莞网站建设东莞常平翔龙天地
  • 长沙创意网站建设网站和搜索引擎
  • 网页游戏网站排行猪八戒小程序开发报价
  • 广州做网站公司电话太原小程序制作电话
  • 别人做的网站域名到期怎么办扬州天达建设集团有限公司网站
  • 模板建站适屏黑龙江网站建设seo优化
  • 做网站公司需要多少钱四川建设银行手机银行下载官方网站下载安装
  • 关于网站建设管理的通知超级软文网
  • 密云做网站的分类信息网站发布标题
  • 重庆网站开发公阿克苏网站开发
  • 做网站后有人抢注关键词软件开发技术服务合同
  • 网站建设公司发展理念计算机培训机构
  • 网站前端如何做兼职网站宣传平台
  • 如何在自己电脑上做网站网站入侵怎么做
  • 手机如何做车载mp3下载网站网址域名查询
  • html5video网站佛山市企业网站seo点击软件
  • 同学会网站建设方案建设工程造价网站