生产者消费者模型的信号量+ReentrantLock的实现

package cn.edu.sysu;

import java.util.ArrayList;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class ObjectQueue{
	
	private final int size=10;
	
	private final Semaphore notEmpty=new Semaphore(0);
	private final Semaphore notFull=new Semaphore(size);
	
	private ArrayList<Integer> ints=new ArrayList<Integer>();
	
	private Lock lock=new ReentrantLock();
	
	public ObjectQueue(){}
	
	public void putItem(Integer i){
		
		try {
			notFull.acquire();
			lock.lock();
			ints.add(i);
			notEmpty.release();
		} catch (Exception e) {
			System.out.println("Exception happened in object queue put item");
		}
		finally{
			lock.unlock();
		}
	}
	
	public Integer getItem(){
		
		try {
			notEmpty.acquire();
			lock.lock();
			Integer i=ints.remove(0);
			notFull.release();
			return i;
		} catch (Exception e) {
			System.out.println("Exception happened in object queue get item");
		}
		finally{
			lock.unlock();
		}
		return null;
	}
}

class Producer extends Thread{
	
	int id;
	ObjectQueue queue;
	public Producer(int i, ObjectQueue q){
		super();
		this.id=i;
		this.queue=q;
	}
	
	public void run() {
		for(int i=0;i<100;i++){
			queue.putItem(new Integer(i+10000));
			System.out.println("Producer "+id+" produce Integer "+(i+10000));
			try {
				Thread.currentThread().sleep(10);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}


class Consumer extends Thread{
	
	int id;
	ObjectQueue queue;
	public Consumer(int i, ObjectQueue q){
		super();
		this.id=i;
		this.queue=q;
	}
	
	public void run() {
		for(int i=0;i<100;i++){
			Integer item=queue.getItem();
			System.out.println("Consumer "+id+" consume Integer "+item.intValue());
			try {
				Thread.currentThread().sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}


public class LockImplmentation {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ObjectQueue queue=new ObjectQueue();
		Producer p=new Producer(1,queue );
		int consumerCount=5;
		Consumer c[]=new Consumer[consumerCount];
		for(int i=0;i<consumerCount;i++){
			c[i]=new Consumer(i, queue);
			c[i].start();
		}
		p.start();
		
	}

}

要把心静下来的话还是应该去写一些基本的程序,虽然很简单,但是不失为笔试面试的有效材料,有点不规范的地方,但是我就是喜欢extends Thread,用Runnable的话好像还是不习惯多写几行。另外的话可以复习一下进程同步的知识


连什么是可重入锁都不知道就在那里瞎写:

可重入锁,最关键的地方是认识他

final Condition con = lock.newCondition();

之后所得到的condition调用await,和直接调用Thread.sleep的区别

前者是lock住了但是不保持这个锁(别人可以保持这个锁,到他回来的时候,就是重入了),后者不光是保持了这个锁,而且lock了这个锁(别人怎么也进不来了)

看这里:http://tenyears.iteye.com/blog/48750


官方:

如果该锁定没有被另一个线程保持,则获取该锁定并立即返回,将锁定的保持计数设置为 1。
如果当前线程已经保持该锁定,则将保持计数加 1,并且该方法立即返回。
如果该锁定被另一个线程保持,则出于线程调度的目的,禁用当前线程,并且在获得锁定之前,该线程将一直处于休眠状态,此时锁定保持计数被设置为 1。

转载于:https://my.oschina.net/zuoyc/blog/82544

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值