码迷,mamicode.com
首页 > 编程语言 > 详细

另类多线程生产者与消费者模式

时间:2017-01-03 20:22:51      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:另类   操作   override   notify   ant   sig   wait   exception   ati   

题目:现在两个线程,可以操作同一个变量,实现一个线程对该变量加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,解决虚假唤醒的问题。



另类多线程生产者与消费者模式

标签:另类   操作   override   notify   ant   sig   wait   exception   ati   

原文地址:http://www.cnblogs.com/baizhanshi/p/6245919.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!