在一个单独的线程中启动zookeeper

时间:2014-08-19 08:33:57

标签: java multithreading configuration distributed apache-zookeeper

我正在尝试以这种方式将多个嵌入式zookeeper服务器从Java应用程序启动到单独的线程中:

String port1 = "2181";
String directory1 = new File(System.getProperty("java.io.tmpdir"), "zookeeper/data1").getAbsolutePath();
final ServerConfig config1 = new ServerConfig();
config1.parse(new String[] { port1, directory1 });
new Thread(new Runnable() {      
  @Override
  public void run()
  {
    try{
      ZooKeeperServerMain zk = new ZooKeeperServerMain()
      zk.runFromConfig(config1);
    }catch(Exception e){
      e.printStackTrace();
    }        
  }
}).run();    

当我以这种方式启动zk时,主进程被阻止,其余指令不会被执行! 有没有正确的方法在单独的线程中启动zookeeper?

1 个答案:

答案 0 :(得分:4)

您在run()上呼叫start()而不是Thread

new Thread(new Runnable() {      
@Override
public void run()
{
  try{
    ZooKeeperServerMain zk = new ZooKeeperServerMain()
    zk.runFromConfig(config1);
  }catch(Exception e){
    e.printStackTrace();
  }        
}
}).start();