首页 > 代码库 > 另类多线程生产者与消费者模式
另类多线程生产者与消费者模式
题目:现在两个线程,可以操作同一个变量,实现一个线程对该变量加1,一个线程对该变量减1,实现交替,来10轮,变量初始值为零。
import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; class ShareData { private int number = 0; Lock lock = new ReentrantLock(); Condition condition = lock.newCondition(); public void increment() throws Exception { lock.lock(); try { while (number != 0) { condition.await();//this.wait(); } ++number; System.out.println(Thread.currentThread().getName() + "\t" + number); condition.signalAll();//this.notifyAll(); } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } public void decrement() throws Exception { lock.lock(); try { while (number == 0) { condition.await();//this.wait(); } --number; System.out.println(Thread.currentThread().getName() + "\t" + number); condition.signalAll();//this.notifyAll(); } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } //========这种写法是最原始的写法使用了synchronized======== /* public synchronized void increment() throws Exception { while(number != 0) { this.wait(); } ++number; System.out.println(Thread.currentThread().getName()+"\t"+number); this.notifyAll(); } public synchronized void decrement() throws Exception { while(number == 0) { this.wait(); } --number; System.out.println(Thread.currentThread().getName()+"\t"+number); this.notifyAll(); } */ } /** * 题目:现在两个线程,可以操作同一个变量,实现一个线程对该变量加1,一个线程对该变量减1, * 实现交替,来10轮,变量初始值为零。 * * @author admin * 创建四个线程操作资源(高内聚,低耦合) */ public class ThreadDemo2 { public static void main(String[] args) { final ShareData sd = new ShareData(); new Thread(new Runnable() { @Override public void run() { for (int i = 1; i <= 10; i++) { try { Thread.sleep(200); sd.increment(); } catch (Exception e) { e.printStackTrace(); } } } }, "AA").start(); new Thread(new Runnable() { @Override public void run() { for (int i = 1; i <= 10; i++) { try { Thread.sleep(300); sd.decrement(); } catch (Exception e) { e.printStackTrace(); } } } }, "BB").start(); new Thread(new Runnable() { @Override public void run() { for (int i = 1; i <= 10; i++) { try { Thread.sleep(400); sd.increment(); } catch (Exception e) { e.printStackTrace(); } } } }, "CC").start(); new Thread(new Runnable() { @Override public void run() { for (int i = 1; i <= 10; i++) { try { Thread.sleep(500); sd.decrement(); } catch (Exception e) { e.printStackTrace(); } } } }, "DD").start(); } }
在这个需要注意一个问题在共享对象的方法里 将if替换为while,解决虚假唤醒的问题。
另类多线程生产者与消费者模式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。