如何使主线程等待另一个用Java完成?

时间:2015-08-05 22:01:00

标签: java multithreading jedis

我已经提出了跟随线程'halo'以使其连接到db(在本例中为redis),并且在服务器失败的情况下,将等待一秒钟并再次尝试。在我的单元测试类中,方法正在执行,并且在新线程启动后不久,服务器将失败。但是这个新线程'晕'会立即关闭。我做错了什么?

// almost infinitely large number of sets, interrupted by server seg-fault
    // you gotta try company methods 
    Thread halo = new Thread(new Runnable() {
        @Override
        public void run() {
            int count = 0;
            while (count < Integer.MAX_VALUE) {
                if (JedisPoolFactory.getStatus()) {
                    try {
                        for (int i = 0; i < 10000; i++) {
                            master.set(String.format("key_%d", count), String.format("value_%d", count));
                            System.out.println(master.get(String.format("key_%d", count)));
                            count++;
                        }
                    } catch (JedisConnectionException igr) {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException ignore) {}
                    }
                } else {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ignrod) {}
                }
            }
        }
    });
    halo.start();

    try {
        master.debug(DebugParams.SEGFAULT());
        halo.join();
    } catch (JedisConnectionException ignored) {
    } catch (InterruptedException igr) {}

1 个答案:

答案 0 :(得分:0)

线程连接应该在异常之外完成,当master进行segfault时,它会调用jedisconnectionexception。