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

题目:现在两个线程,可以操作同一个变量,实现一个线程对该变量加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() + "	" + 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() + "	" + 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()+"	"+number);
		this.notifyAll();
	}
	
	public synchronized void decrement() throws Exception
	{
		while(number == 0)
		{
			this.wait();
		}
		--number;
		System.out.println(Thread.currentThread().getName()+"	"+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,解决虚假唤醒的问题。



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