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

网页显示站点不安全软文推广例子

网页显示站点不安全,软文推广例子,网站建设教程浩森宇特,最新的电商资讯有哪些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.mmbaike.com/news/71834.html

相关文章:

  • 网站域名证书哪里获取爱站网长尾关键词搜索
  • 做网站平台的营业执照广州软文推广公司
  • 斗门区住房和城乡建设网站锦州seo推广
  • 网站怎么做移动端成都网络优化托管公司
  • 公司网站怎么做备案seo网站优化推广费用
  • 湖北网站建设公司怎么做网络推广最有效
  • 邯郸做网站哪家好bt磁力链好用的引擎
  • 深圳58同城网站建设如何模板建站
  • 民非单位年检那个网站做烟台网络推广
  • 有没有介绍做私家导游的网站企业培训考试系统
  • 橙色网站模板优化网站技术
  • 2017年网站外链怎么做seo推广价格
  • 台前网站建设属于b2b的网站有哪些
  • 免费申请手机号苏州seo怎么做
  • 人大网站 建设宁波seo外包代运营
  • 用c 做网站免费的短视频app大全
  • 义乌国贸学校网站建设深圳外贸网站制作
  • 网站ui设计之道网络营销的四种方式
  • 上传网站标志必应搜索引擎入口
  • 交通建设集团蓝商分公司网站徐州百度推广电话
  • 网站做全景图预览微信软文案例
  • 咸阳seo西安网站seo费用
  • 自己做网站能赚钱汕头seo代理商
  • 淘宝天猫优惠券网站怎么做苏州百度 seo
  • 手机网站404页面模板营销策划方案
  • 门窗网站制作宣传语建站服务
  • 武汉工商网站查询企业信息企业培训课程表
  • 做网站累吗宁波seo入门教程
  • 微信的微网站互联网营销公司
  • 手机网站在后台怎么做编辑内江seo