码迷,mamicode.com
首页 > 其他好文 > 详细

Semaphore(计数信号量)

时间:2016-09-21 22:54:41      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

//对象池
public
class Pool<T> { private int size; private List<T> items = new ArrayList<T>(); private volatile boolean[] checkedOut; private Semaphore available; public Pool(Class<T> classObject,int size) { this.size = size; this.checkedOut = new boolean[size]; this.available = new Semaphore(size, true); for (int i = 0; i < size; i++) { try { items.add(classObject.newInstance()); } catch (Exception e) { throw new RuntimeException(e); } } } public T checkOut() throws InterruptedException{ available.acquire(); return getItem(); } public void checkIn(T x){ if(releaseItem(x)){ available.release(); } } private synchronized T getItem(){ for (int i = 0; i < size; ++i) { if(!checkedOut[i]){ checkedOut[i] = true; return items.get(i); } } return null; } private synchronized boolean releaseItem(T item){ int index = items.indexOf(item); if(index == -1){ return false; } if(checkedOut[index]){ checkedOut[index] = false; return true; } return false; } }



//签出任务
class
CheckoutTask<T> implements Runnable{ private static int counter = 0; private final int id = counter++; private Pool<T> pool; public CheckoutTask(Pool<T> pool) { this.pool = pool; } @Override public void run() { try { T item = pool.checkOut(); System.out.println(this + "checked out " + item); TimeUnit.SECONDS.sleep(1); System.out.println(this + "checked in " + item); pool.checkIn(item); } catch (InterruptedException e) { e.printStackTrace(); } } @Override public String toString() { return "CheckoutTask " + id + " "; } } public class SemaphoreDemo { final static int SIZE = 25; public static void main(String[] args) throws InterruptedException { final Pool<Fat> pool = new Pool<Fat>(Fat.class, SIZE); ExecutorService exec = Executors.newCachedThreadPool(); for (int i = 0; i < SIZE; i++) { exec.execute(new CheckoutTask<Fat>(pool)); } System.out.println("All CheckoutTask created"); List<Fat> list = new ArrayList<Fat>(); System.out.println("kifjdskjfjdk"); for (int i = 0; i < SIZE; i++) { Fat f = pool.checkOut(); System.out.println(i + ":main() thread checked out "); f.operation(); list.add(f); } Future<?> blocked = exec.submit(new Runnable() { @Override public void run() { try { pool.checkOut(); } catch (InterruptedException e) { System.out.println("checkedOut() Interrupted"); } } }); TimeUnit.SECONDS.sleep(2); blocked.cancel(true); System.out.println("Checking in objects in " + list); for (Fat f : list) { pool.checkIn(f); } for (Fat f : list) { pool.checkIn(f); } exec.shutdown(); } }

 

public class Fat {
    private volatile double d;
    private static int counter = 0;
    private final int id = counter++;
    public Fat() {
        for (int i = 0; i <10000; i++) {
            d +=(Math.PI + Math.E) / i;
        }
    }
    public void operation(){
        System.out.println(this);
    }
    @Override
    public String toString() {
        return "Fat id: " + id;
    }
}

 



Semaphore(计数信号量)

标签:

原文地址:http://www.cnblogs.com/wangjun2016/p/wangjun201609212134.html

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