GenericObjectPoolsborrowObject 方法线程安全吗? [英] Is GenericObjectPools borrowObject Method thread safe?

查看:97
本文介绍了GenericObjectPoolsborrowObject 方法线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个问题中 Is GenericObjectPool来自 commons.apache.org 线程安全? 提到它的线程安全.

In this question Is GenericObjectPool<T> from commons.apache.org thread safe? It is mentioned that its thread safe .

已但是我的多线程应用程序中出现了两个线程同时从池中获取同一个对象的情况.-此语句是错误的.

Edited:But im having a situation in my multithreaded application that two threads are getting the same object from the pool at the same time.-This statement was wrong.

我移动了借用对象来同步块,它解决了我的问题.

I moved the borrowObject to synchronize block and it solved my issue.

有人之前遇到过这个问题吗?

Has anyone faced this issue earlier?

这是我的代码:

public static GenericObjectPool<IDocBuilderPool> documentBuilderPool = new GenericObjectPool(new DocumentPool());

static {
    documentBuilderPool.setMaxActive(1000);
    documentBuilderPool.setMaxWait(30000);
    documentBuilderPool.setMaxIdle(-1);

}
//method that returns document pool called by multiple threads .

public static IDocBuilderPool getDocumentPool() {

    return documentBuilderPool.borrowObject();
}

//The pool factory class
public class DocumentPool extends BasePoolableObjectFactory<ICollabrrDocument> {

    public DomDocumentPool() {
    }

    @Override
    public DomDocument makeObject() throws Exception {
        // TODO Auto-generated method stub
        return new DomDocument();
    }

    @Override
    public void activateObject(IDocBuilderPool obj) throws Exception {
        // TODO Auto-generated method stub
        super.activateObject(obj);
    }

    @Override
    public void destroyObject(IDocBuilderPool obj) throws Exception {
        // TODO Auto-generated method stub
        super.destroyObject(obj);

    }

    @Override
    public void passivateObject(IDocBuilderPool obj) throws Exception {
        // TODO Auto-generated method stub
        obj.release();
        super.passivateObject(obj);
    }

    @Override
    public boolean validateObject(IDocBuilderPool obj) {
        // TODO Auto-generated method stub
        return super.validateObject(obj);
    }
}




public class DomDocument implements IDocBuilderPool  {

private Document domDocument;
private DocumentBuilder documentBuilder;
private DocumentBuilderFactory documentBuilderFactory;

public HashMap<org.w3c.dom.Node, DOMElement> elementMap = new HashMap<org.w3c.dom.Node, DOMElement>();

public long threadID;

public DomDocument()  {


    setDomDocument();
    this.threadID = Thread.currentThread().getId();

}

public void setDomDocument() throws 
    this.documentBuilderFactory = DocumentBuilderFactory.newInstance();


        this.documentBuilderFactory.setNamespaceAware(true);
        this.documentBuilder = this.documentBuilderFactory.newDocumentBuilder();
        this.domDocument = this.documentBuilder.parse(new ByteArrayInputStream("<Root/>".getBytes()));


}

}

推荐答案

PoolableObjectFactory 的文档 状态:

PoolableObjectFactory 必须是线程安全的.

PoolableObjectFactory must be thread-safe.

查看您的代码,唯一可能是线程不安全的是对 obj.release(); 的调用.这可能是您的问题所在.

Looking at your code, the only thing that could be thread unsafe is the call to obj.release();. This is possibly where your problem is.

除此之外,一切看起来都还不错...

Apart from that all looks ok...

这篇关于GenericObjectPoolsborrowObject 方法线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆