星空网 > 软件开发 > Java

一个线程加一运算,一个线程做减一运算,多个线程同时交替运行

使用synchronized

 

 

package com.pb.thread.demo5;/**使用synchronized * 一个线程加一运算,一个线程做减法运算,多个线程同时交替运行 * * @author Denny * */public class Count {  private int num = 0;  private boolean flag = false; // 标识  //加法  public synchronized void add() {    while (flag) {      try {        wait();      } catch (InterruptedException e) {        e.printStackTrace();      }    }    this.num++; //加    System.out.println(Thread.currentThread().getName() + "........" + this.num);    this.flag=true; //设置标识为true    notifyAll(); //唤醒所有在线程池中冻结的线程,会把所有都唤醒      }  //减法  public synchronized void sub() {    while (!flag) {      try {        wait();      } catch (InterruptedException e) {        e.printStackTrace();      }    }    this.num--; //减    System.out.println(Thread.currentThread().getName() + "........" + this.num);    this.flag=false; //设置标识为true    notifyAll(); //唤醒所有在线程池中冻结的线程,会把所有都唤醒  }}

 

 

 

 

package com.pb.thread.demo5;public class Add implements Runnable {  private Count count;  public Add(Count count){    this.count=count;  }   @Override  public void run() {    while(true){      count.add();    }  }}//================package com.pb.thread.demo5;public class Sub implements Runnable {  private Count count;  public Sub(Count count){    this.count=count;  }   @Override  public void run() {    while(true){      count.sub();    }  }} 

 

测试类

 

package com.pb.thread.demo5;public class CountTest {  public static void main(String[] args) {    Count c=new Count();    Add add=new Add(c);    Sub sub=new Sub(c);    Thread t1=new Thread(add);    Thread t2=new Thread(add);    Thread t3=new Thread(sub);    Thread t4=new Thread(sub);    t1.start();    t2.start();    t3.start();    t4.start();  }}

 

结果:

 
Thread-2........0Thread-1........1Thread-3........0Thread-0........1Thread-2........0Thread-1........1Thread-3........0Thread-0........1Thread-2........0
 

不使用synchronized

package com.pb.thread.demo4;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;/** * 一个线程加一运算,一个线程做减法运算,多个线程同时交替运行 * @author Denny * */public class Count {  private int num = 0;  private boolean flag=false; // 标识  Lock lock = new ReentrantLock(); // 锁  Condition add = lock.newCondition(); // 加法锁  Condition sub = lock.newCondition();// 减法锁  public void add() {    lock.lock();// 锁上    try {      while (flag) { //循环判断        add.await();      }      this.num++;      System.out.println(Thread.currentThread().getName() + "........" + this.num);      this.flag = true; // 设置标识      sub.signal(); // 唤醒指定线程    } catch (InterruptedException e) {      e.printStackTrace();    }finally{      lock.unlock();    }      }  public void sub() {    lock.lock();// 锁上    try {      while (!flag) {//循环判断        sub.await();      }      this.num--;      System.out.println(Thread.currentThread().getName() + "........" + this.num);      this.flag = false; // 设置标识      add.signal(); // 唤醒指定线程    } catch (InterruptedException e) {      e.printStackTrace();    }finally{      lock.unlock();    }      }}

 

 

 

package com.pb.thread.demo4;public class Add implements Runnable {  private Count count;  public Add(Count count){    this.count=count;  }   @Override  public void run() {    while(true){      count.add();    }  }}

 

 

package com.pb.thread.demo4;public class Sub implements Runnable {  private Count count;  public Sub(Count count){    this.count=count;  }   @Override  public void run() {    while(true){      count.sub();    }  }}

 

 

package com.pb.thread.demo4;public class CountTest {  public static void main(String[] args) {    Count c=new Count();    Add add=new Add(c);        Sub sub=new Sub(c);    Thread t1=new Thread(add);    Thread t2=new Thread(add);    Thread t3=new Thread(sub);    Thread t4=new Thread(sub);    t1.start();    t2.start();    t3.start();    t4.start();  }}

 

结果:

 
Thread-1........1Thread-3........0Thread-0........1Thread-2........0Thread-1........1Thread-3........0Thread-0........1Thread-2........0
 

 




原标题:一个线程加一运算,一个线程做减一运算,多个线程同时交替运行

关键词:线程

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

突发!一艘满载中国出口货物的超大型集装箱船遇险,大量货柜落水:https://www.kjdsnews.com/a/1699354.html
圣诞被海外疯抢,上万网友直呼我想要!:https://www.kjdsnews.com/a/1699355.html
美国电商市场大变动! Temu 和 Shein 打下半壁江山!:https://www.kjdsnews.com/a/1699356.html
美国官网商标新版搜索介绍及如何使用:https://www.kjdsnews.com/a/1699357.html
Otto圣诞购物季销售破记录,每小时处理25000个包裹:https://www.kjdsnews.com/a/1699358.html
Lululemon将于年底完全停产健身镜业务Mirror:https://www.kjdsnews.com/a/1699359.html
跨境支付百科——巴西支付篇:https://www.kjdsnews.com/a/1836648.html
大福地快捷酒店预订 大福酒店怎么走:https://www.vstour.cn/a/365187.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流