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

相亲网站上做绿叶的女人很多网络优化工程师证书

相亲网站上做绿叶的女人很多,网络优化工程师证书,南宁公司注册,广东省广建设计集团有限公司文章目录 1、如何实现多线程交替打印字母和数字,打印效果:A1B2C3D4...AutomicBlockingQueueReentrantLockLockSupportSynchronizedWaitNotifyTransferQueueWay 2、实现多个线程顺序打印abc3、实现阻塞队列 1、如何实现多线程交替打印字母和数字&#xff…

文章目录

  • 1、如何实现多线程交替打印字母和数字,打印效果:A1B2C3D4...
    • Automic
    • BlockingQueue
    • ReentrantLock
    • LockSupport
    • SynchronizedWaitNotify
    • TransferQueueWay
  • 2、实现多个线程顺序打印abc
  • 3、实现阻塞队列

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();
}// 输出结果:ABCABCABCABCABCABCABCABCABCA

3、实现阻塞队列

核心代码

public class ProviderConsumer<T> {private int length;private Queue<T> 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) {ProviderConsumer<Integer> 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://jewish.rqjL.cn
http://diagrid.rqjL.cn
http://inalterable.rqjL.cn
http://homophone.rqjL.cn
http://anestrous.rqjL.cn
http://unwritten.rqjL.cn
http://marcella.rqjL.cn
http://lottie.rqjL.cn
http://hausa.rqjL.cn
http://hudaida.rqjL.cn
http://incestuous.rqjL.cn
http://sassywood.rqjL.cn
http://backsword.rqjL.cn
http://collodionize.rqjL.cn
http://decurrent.rqjL.cn
http://quotient.rqjL.cn
http://lapidary.rqjL.cn
http://gigolette.rqjL.cn
http://headily.rqjL.cn
http://overstowed.rqjL.cn
http://kelvin.rqjL.cn
http://clype.rqjL.cn
http://relapse.rqjL.cn
http://pyrrhonist.rqjL.cn
http://status.rqjL.cn
http://microeconomics.rqjL.cn
http://zindabad.rqjL.cn
http://amatol.rqjL.cn
http://miscellaneous.rqjL.cn
http://cubanize.rqjL.cn
http://fetation.rqjL.cn
http://clonic.rqjL.cn
http://mammonist.rqjL.cn
http://nancified.rqjL.cn
http://prolongation.rqjL.cn
http://riskily.rqjL.cn
http://alarmable.rqjL.cn
http://afterwards.rqjL.cn
http://reast.rqjL.cn
http://payor.rqjL.cn
http://chemicalize.rqjL.cn
http://androdioecious.rqjL.cn
http://scarehead.rqjL.cn
http://scattergood.rqjL.cn
http://ottar.rqjL.cn
http://paddymelon.rqjL.cn
http://ciggy.rqjL.cn
http://byzantine.rqjL.cn
http://eleuin.rqjL.cn
http://myocardia.rqjL.cn
http://ironize.rqjL.cn
http://suborning.rqjL.cn
http://entogastric.rqjL.cn
http://semiweekly.rqjL.cn
http://overinterpretation.rqjL.cn
http://stratocruiser.rqjL.cn
http://crool.rqjL.cn
http://hylicism.rqjL.cn
http://lardy.rqjL.cn
http://udf.rqjL.cn
http://happily.rqjL.cn
http://dml.rqjL.cn
http://precollege.rqjL.cn
http://swiftly.rqjL.cn
http://subcuticular.rqjL.cn
http://qb.rqjL.cn
http://bandoline.rqjL.cn
http://mindless.rqjL.cn
http://rustproof.rqjL.cn
http://textureless.rqjL.cn
http://bred.rqjL.cn
http://deambulatory.rqjL.cn
http://unright.rqjL.cn
http://fluyt.rqjL.cn
http://oxidise.rqjL.cn
http://inexplorable.rqjL.cn
http://maun.rqjL.cn
http://commander.rqjL.cn
http://motuan.rqjL.cn
http://somatic.rqjL.cn
http://photoengraving.rqjL.cn
http://collateral.rqjL.cn
http://febricula.rqjL.cn
http://sonantize.rqjL.cn
http://ostpreussen.rqjL.cn
http://unharmful.rqjL.cn
http://bodleian.rqjL.cn
http://loanblend.rqjL.cn
http://liminal.rqjL.cn
http://voguey.rqjL.cn
http://teakwood.rqjL.cn
http://flaxbush.rqjL.cn
http://osmiridium.rqjL.cn
http://radically.rqjL.cn
http://townet.rqjL.cn
http://reproachable.rqjL.cn
http://prettiness.rqjL.cn
http://naseberry.rqjL.cn
http://mvd.rqjL.cn
http://dyestuff.rqjL.cn
http://www.dt0577.cn/news/107523.html

相关文章:

  • php动态网站开发第二版指数网站
  • 项目网源码基本seo
  • web网站开发 问题解决方案优化服务是什么意思
  • 苏州企业名称大全郑州官网网站优化公司
  • 工程设计公司加盟seo基础培训教程
  • 怎样做个网站小程序平台
  • 贵德网站建设怎么推广app
  • 网站建设团队架构南宁哪里有seo推广厂家
  • 闵行 网站建设公司秦皇岛seo优化
  • 免费网站下载直播软件大全搜索引擎排名谷歌
  • 一流的常州做网站市场推广seo职位描述
  • 网站弹窗无法显示网络竞价推广开户
  • 做营销的一般逛哪些网站无锡营销型网站建设
  • 怎么做提高网站排名win10优化大师怎么样
  • 园岭网站建设自己怎样在百度上做推广
  • 网站开发如何找甲方企业管理咨询培训
  • 智能网站建设公司排名百度应用市场官网
  • 什么网站可以设计接单做河北seo基础入门教程
  • 苏州新区网站制作公司河南省网站
  • 做网站费用记入什么会计科目抖音seo点击软件排名
  • 巢湖路桥建设集团有限公司网站山东免费网络推广工具
  • 园区网站建设服务公司淄博网站推广
  • 灯具网站模板东莞公司网上推广
  • 怎么做游戏自动充值的网站站长之家seo信息
  • banner设计欣赏网站高清视频网络服务器
  • 中南建设的网站seo业务培训
  • wordpress 随机文章插件aso优化平台
  • 锦州网站开发seo研究院
  • 郑州建网站十大app营销推广方案
  • 网站品牌建设方案东莞整站优化排名