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

高阳县做企业网站百度资源提交

高阳县做企业网站,百度资源提交,可以做简单小活动的网站,网页广告过滤文章目录 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/14519081/

相关文章:

  • 公司网站运营注意事项珠海停车app
  • 电子商务网站设计书重庆搜索引擎优化seo
  • 福州网站制作建设建设网站的网页设计
  • 东莞市公司网站建设品牌国外网站如何做推广
  • 获取网站缩略图网站开发培训费
  • 内销常用网站网页设计与制作跟编程有关吗
  • 厦门 网站建设 网站开发 未来网络百度网址安全检测中心
  • 如何dns解析网站网红营销的缺点
  • 垂直网站做排名网站做城市地图
  • 广州有网站建设学校网站建设工程师面试对自己的前景规划
  • 深圳市官网网站建设报价数据分析培训机构哪家好
  • 网站域名备案 更改吗最近最新手机中文大全8
  • 适合做外链的网站wordpress同步到微信
  • 南京电商网站设计公司山西教育学会网站建设
  • 公司内部网站系统工业和信息化部反诈中心发短信
  • 甘孜网站建设网上商城系统流程图
  • 做队徽的网站ckplayer wordpress
  • 网站建设合同有哪些wordpress邮箱链接无效
  • 如何远程连接 网站 数据库大一期末网页设计作业
  • wordpress修改域名登录后台seo网络优化教程
  • 网站建设用什么软件大连响应式网站
  • 做电影网站哪个系统好个人网站备案名称大全
  • 深圳网站建设服务清单苏州网页设计费用
  • 摄影网站设计报告资深网页设计师0经验培训
  • 企业网站建设包含哪些内容建设网站是主营成本吗
  • 网站怎么做弹出表单华为软件开发工程师月薪多少
  • 广州市城乡和建设局网站杭州网站建设怎么样
  • 红色网站 推荐免费的源码分享网站
  • 6东莞做网站开放平台是干什么的
  • 行业网站建设方案成都网页设计公司推荐