package com.pb.thread.demo5;/**使用synchronized * 一個(gè)線程加一運(yùn)算,一個(gè)線程做減法運(yùn)算,多個(gè)線程同時(shí)交替運(yùn)行 * * @author Denny * */public class Count { PRivate int num = 0; private boolean flag = false; // 標(biāo)識(shí) //加法 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; //設(shè)置標(biāo)識(shí)為true notifyAll(); //喚醒所有在線程池中凍結(jié)的線程,會(huì)把所有都喚醒 } //減法 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; //設(shè)置標(biāo)識(shí)為true notifyAll(); //喚醒所有在線程池中凍結(jié)的線程,會(huì)把所有都喚醒 }}
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(); } }}
測(cè)試類
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(); }}
結(jié)果:
Thread-2........0Thread-1........1Thread-3........0Thread-0........1Thread-2........0Thread-1........1Thread-3........0Thread-0........1Thread-2........0
package com.pb.thread.demo4;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;/** * 一個(gè)線程加一運(yùn)算,一個(gè)線程做減法運(yùn)算,多個(gè)線程同時(shí)交替運(yùn)行 * @author Denny * */public class Count { private int num = 0; private boolean flag=false; // 標(biāo)識(shí) Lock lock = new ReentrantLock(); // 鎖 Condition add = lock.newCondition(); // 加法鎖 Condition sub = lock.newCondition();// 減法鎖 public void add() { lock.lock();// 鎖上 try { while (flag) { //循環(huán)判斷 add.await(); } this.num++; System.out.println(Thread.currentThread().getName() + "........" + this.num); this.flag = true; // 設(shè)置標(biāo)識(shí) sub.signal(); // 喚醒指定線程 } catch (InterruptedException e) { e.printStackTrace(); }finally{ lock.unlock(); } } public void sub() { lock.lock();// 鎖上 try { while (!flag) {//循環(huán)判斷 sub.await(); } this.num--; System.out.println(Thread.currentThread().getName() + "........" + this.num); this.flag = false; // 設(shè)置標(biāo)識(shí) 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
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注