繁体   English   中英

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

[英]how to make main thread wait for another one to finish in Java?

我想出了以下线程“ halo”以使其连接到db(在这种情况下为redis),如果服务器发生故障,将等待一秒钟,然后重试。 在我的单元测试类中,方法正在执行,并且新线程启动后不久,服务器将失败。 但是,此新线程“ halo”立即关闭。 我究竟做错了什么?

// 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) {}

线程连接应该在异常之外进行,当主节点发生段故障时,它将调用jedisconnectionexception。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM