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

广州制作网站公司wordpress登录

广州制作网站公司,wordpress登录,信和财富网站开发,网站建设冷色调threadsynchronized 就好像一个圆圈,A->B->C->A。。。。。 synchronized能够保证多个线程进入实,只用一个线程能进入。 /**多线程交替打印* */ public class Task {private final Object lock new Object();private int count 0;public st…

thread+synchronized

就好像一个圆圈,A->B->C->A。。。。。

synchronized能够保证多个线程进入实,只用一个线程能进入。

/**多线程交替打印* */
public class Task {private final Object lock = new Object();private int count = 0;public static void main(String[] args) {Task task = new Task();new Thread(()->{for(int i=0;i<10;i++){task.printA();}}).start();new Thread(()->{for(int i=0;i<10;i++){task.printB();}}).start();new Thread(()->{for(int i=0;i<10;i++){task.printC();}}).start();}public void printA(){synchronized (lock){while(count!=0){try {lock.wait();} catch (InterruptedException e) {e.printStackTrace();}}System.out.print("A");count++;lock.notifyAll();}}public void printB(){synchronized (lock){while(count!=1){try{lock.wait();}catch (InterruptedException e){e.printStackTrace();}}System.out.print("B");count++;lock.notifyAll();}}public void printC(){synchronized (lock){while(count!=2){try {lock.wait();} catch (InterruptedException e) {e.printStackTrace();}}System.out.print("C");count=0;lock.notifyAll();}}
}

ReentrantLock

public class Task2 {private  final Lock lock=new ReentrantLock();private final Condition condition=lock.newCondition();private final Condition condition1=lock.newCondition();private final Condition condition2=lock.newCondition();private int state=0;public static void main(String[] args) {Task2 tash2 = new Task2();Thread thread = new Thread(tash2::printA);Thread thread1 = new Thread(tash2::printB);thread.start();thread1.start();}private void printA(){// get locklock.lock();try{for(int i=0;i<10;i++){while(state!=0){condition.await();}System.out.print("a");state=1;condition1.signal();}} catch (InterruptedException e) {throw new RuntimeException(e);}finally {lock.unlock();}}private  void printB(){lock.lock();try{for(int i=0;i<10;i++){while(state!=1){condition1.await();}System.out.print("b");state=0;condition.signal();}} catch (InterruptedException e) {throw new RuntimeException(e);}finally {lock.unlock();}}
}

Semaphore

信号量机制,设置刚开始的值为1,如果减去1的结果>=0,则执行该线程,否则不执行,执行完释放,值加1

public final void acquireShared(int arg) {if (tryAcquireShared(arg) < 0)acquire(null, arg, true, false, false, 0L);}/*** Acquires in shared mode, aborting if interrupted.  Implemented* by first checking interrupt status, then invoking at least once* {@link #tryAcquireShared}, returning on success.  Otherwise the* thread is queued, possibly repeatedly blocking and unblocking,* invoking {@link #tryAcquireShared} until success or the thread* is interrupted.* @param arg the acquire argument.* This value is conveyed to {@link #tryAcquireShared} but is* otherwise uninterpreted and can represent anything* you like.* @throws InterruptedException if the current thread is interrupted*/public final void acquireSharedInterruptibly(int arg)throws InterruptedException {if (Thread.interrupted() ||(tryAcquireShared(arg) < 0 &&acquire(null, arg, true, true, false, 0L) < 0))throw new InterruptedException();}
package com.r.ThreadTask;import javax.swing.text.Segment;
import java.util.concurrent.Semaphore;public class Task3 {private final Semaphore semaphore=new Semaphore(1);private final Semaphore semaphore1=new Semaphore(0);private final Semaphore semaphore2=new Semaphore(0);public static void main(String[] args) {Task3 task3 = new Task3();Thread thread = new Thread(task3::printA);Thread thread1 = new Thread(task3::printB);Thread thread2 = new Thread(task3::printC);thread.start();thread1.start();thread2.start();}private void printA(){for(int i=0;i<10;i++){try {semaphore.acquire();} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.print("a");semaphore1.release();}}private void printB(){for(int i=0;i<10;i++){try {semaphore1.acquire();} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.print("b");semaphore2.release();}}private void printC(){for(int i=0;i<10;i++){try {semaphore2.acquire();} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.print("c");semaphore.release();}}
}

http://www.yayakq.cn/news/342441/

相关文章:

  • 合肥网站设计点击到达网站指定位置怎么做
  • 厦门网站建设维护站群cms
  • wordpress文章图片点击放大预览seo优化在哪里学
  • 汕头网站推广哪家好游戏开发需要学什么专业
  • 东莞网站设计网址中国铁路建设投资公司网站
  • 做网站淄博核桃编程加盟费多少钱
  • 株洲网站建设和制作浙江省城乡建设信息港
  • 网站设计制作软件网站app充值记账凭证怎么做
  • 化妆品做备案的网站石家庄网站建设系统
  • 网站建设流程步骤高级软件开发工程师证书含金量
  • 做网站基本东西网易网站建设
  • 凡科建站电脑版网址新品牌推广策略
  • 内网网站建设的亮点特点网站建设成都公司
  • 有什么平台做网站比较好用微信微博网站来做睡眠经济
  • 改wordpress地址还是站点地址常州百度推广代理公司
  • 制作自己盈利的网站海口财务
  • 网站优化软件网站建设岗位职责
  • 建设部网站查询造价师证件国内专业的seo机构
  • 免费软件下载网站排行网站推广计划怎么写
  • 免费个人网站建设苏州企业服务平台
  • 网站的规划 建设与分析论文wordpress 浏览器缓存
  • 购物网站开发的难点山石网站超市
  • php 网站开发框架ap福州市交通建设集团有限公司网站
  • 深圳网站建设公司乐云seo做婚纱网站的目的
  • h5网站价格方案wordpress 301定向
  • 设计网站大全湖南岚鸿设计关键词排名软件官网
  • 怎么做软文网站用iis制作简单网站
  • 淘宝客免费建网站网站建设服务器都有哪些
  • 桓台网站制作衡水市住房和城乡规划建设网站
  • 为某公司或企业做的门户网站北京免费建站