多线程时设置SVNKit

时间:2010-09-13 17:16:27

标签: java multithreading svnkit

我正在尝试使用不同的线程并行连接到许多SVN存储库,使用SVNKit。

在线查看一些代码示例,看起来在使用SVNKit之前我必须使用静态方法初始化它

DAVRepositoryFactory.setup();
SVNRepositoryFactoryImpl.setup();
FSRepositoryFactory.setup();

显然静态方法让我关注多线程环境。我的问题是:

  1. 是否有可能以这种方式并行使用SVNKit?
  2. 我什么时候需要调用这些设置方法?只有在软件的开头,每个线程一次,什么?
  3. 如果有人能解释我必须称这些方法的原因,我也很高兴。

1 个答案:

答案 0 :(得分:2)

在不同的线程中创建存储库实例之前,您只需要调用此方法一次。

来自SVNRepositoryFactoryImpl javadoc:

  

在使用库之前在应用程序中执行一次,可以通过svn-protocol(通过svn和svn + ssh)使用存储库

以下是包含2个存储库(monothread)的示例代码:

SVNRepositoryFactoryImpl.setup(); // ONCE!

String url1 = "svn://host1/path1";
SVNRepository repository1 = SVNRepositoryFactory.create(SVNURL.parseURIDecoded(url1));
String url2 = "svn://host2/path2";
SVNRepository repository2 = SVNRepositoryFactory.create(SVNURL.parseURIDecoded(url2));

在多线程环境中,您可以创建一个实现Runnable的类:

public class ProcessSVN implements Runnable {

    private String url;

    public ProcessSVN(String url) {
        this.url = url;
    }

    public void run() {
        SVNRepository repository = SVNRepositoryFactory.create(SVNURL.parseURIDecoded(url));
        // do stuff with repository
    }
}

并像这样使用它:

SVNRepositoryFactoryImpl.setup(); // STILL ONCE!

(new Thread(new ProcessSVN("http://svnurl1"))).start();
(new Thread(new ProcessSVN("http://svnurl2"))).start();