本文介绍了这个基本的Java对象池是否工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下基本对象池是否工作?我有一个更复杂的一个基于相同的想法(即维护一个信号量和一个BlockingQueue)。我的问题是 - 我需要信号量和BlockingQueue吗?我是不是我不需要做任何同步?

  import java.util.Collection; 
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Semaphore;

public final class Pool< T> {

private final BlockingQueue< T>对象;
私人最终信号量许可证;

public Pool(Collection< ;? extends T> objects){
//我们拥有与池中的对象一样多的许可:
this.permits = new Semaphore(objects。尺寸());
this.objects = new ArrayBlockingQueue< T>(objects.size(),false,objects);
}

public T borrow(){
this.permits.acquireUninterruptibly();
//我们有一个许可证,所以必须有一个:
return this.objects.poll();
}

public void giveBack(T object){
this.objects.add(object);
this.permits.release();
}
}


解决方案

已经指出,单独的有界BlockingQueue就足够了。例如,下面的代码会做你想要的:

  import java.util.Collection; 
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public final class Pool< T> {

private final BlockingQueue< T>对象;

public Pool(Collection< ;? extends T> objects){
this.objects = new ArrayBlockingQueue< T>(objects.size(),false,objects);
}

public T borrow()throws InterruptedException {
return this.objects.take();
}

public void giveBack(T object)throws InterruptedException {
this.objects.put(object);
}
}

此外,您可能需要考虑支持定时版本如果你没有一个有界的阻塞队列数据结构,那么你可以在任何数据的顶部施加一个信号量结构来创建线程安全和绑定的行为。


Does the following basic object pool work? I have a more sophisticated one based on the same idea (i.e. maintaining both a Semaphore and a BlockingQueue). My question is - do I need both Semaphore and BlockingQueue? Am I right that I don't need to do any synchronisation?

import java.util.Collection;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Semaphore;

public final class Pool<T> {

    private final BlockingQueue<T> objects;
    private final Semaphore permits;

    public Pool(Collection<? extends T> objects) {
        // we have as many permits as objects in our pool:
        this.permits = new Semaphore(objects.size());
        this.objects = new ArrayBlockingQueue<T>(objects.size(), false, objects);
    }

    public T borrow() {
        this.permits.acquireUninterruptibly();
        // we have a permit, so there must be one in there:
        return this.objects.poll();
    }

    public void giveBack(T object) {
        this.objects.add(object);
        this.permits.release();
    }
}
解决方案

As has been pointed out, a bounded BlockingQueue alone would be sufficient. For example, the following code will do what you want:

import java.util.Collection;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public final class Pool<T> {

    private final BlockingQueue<T> objects;

    public Pool(Collection<? extends T> objects) {
        this.objects = new ArrayBlockingQueue<T>(objects.size(), false, objects);
    }

    public T borrow() throws InterruptedException {
        return this.objects.take();
    }

    public void giveBack(T object) throws InterruptedException {
        this.objects.put(object);
    }
}

Also, you might want to consider supporting a timed version of borrow() using BlockingQueue.poll().

If you didn't have a bounded blocking queue data structure, then you can impose a semaphore on top of any data structure to create a thread safe and bound behavior.

这篇关于这个基本的Java对象池是否工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 09:37