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的话好像仍是不习惯多写几行。另外的话能够复习一下进程同步的知识 java
连什么是可重入锁都不知道就在那里瞎写:
面试
可重入锁,最关键的地方是认识他 app
final Condition con = lock.newCondition(); ui
以后所获得的condition调用await,和直接调用Thread.sleep的区别 this
前者是lock住了可是不保持这个锁(别人能够保持这个锁,到他回来的时候,就是重入了),后者不光是保持了这个锁,并且lock了这个锁(别人怎么也进不来了) spa
看这里:http://tenyears.iteye.com/blog/48750 线程
官方: code
若是该锁定没有被另外一个线程保持,则获取该锁定并当即返回,将锁定的保持计数设置为 1。
若是当前线程已经保持该锁定,则将保持计数加 1,而且该方法当即返回。
若是该锁定被另外一个线程保持,则出于线程调度的目的,禁用当前线程,而且在得到锁定以前,该线程将一直处于休眠状态,此时锁定保持计数被设置为 1。
blog