一个线程加一运算,一个线程做减一运算,多个线程同时交替运行--synchronized...
生活随笔
收集整理的這篇文章主要介紹了
一个线程加一运算,一个线程做减一运算,多个线程同时交替运行--synchronized...
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用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; //設置標識為truenotifyAll(); //喚醒所有在線程池中凍結的線程,會把所有都喚醒 }//減法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; //設置標識為truenotifyAll(); //喚醒所有在線程池中凍結的線程,會把所有都喚醒 } }?
?
?
?
package com.pb.thread.demo5;public class Add implements Runnable {private Count count;public Add(Count count){this.count=count;} @Overridepublic 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;} @Overridepublic 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........0 Thread-1........1 Thread-3........0 Thread-0........1 Thread-2........0 Thread-1........1 Thread-3........0 Thread-0........1 Thread-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;} @Overridepublic 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;} @Overridepublic 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........1 Thread-3........0 Thread-0........1 Thread-2........0 Thread-1........1 Thread-3........0 Thread-0........1 Thread-2........0?
轉載于:https://www.cnblogs.com/liunanjava/p/4822062.html
總結
以上是生活随笔為你收集整理的一个线程加一运算,一个线程做减一运算,多个线程同时交替运行--synchronized...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Xcode版本更新后插件失效解决办法
- 下一篇: java对象的序列化机制详解