简体   繁体   中英

Run multiple threads and stop once one has stopped

I'd like to start a couple of threads that will try to connect to servers.

ArrayList<Thread> stack = new ArrayList<Thread>();
stack.add(new Thread() {
    public void run()
    {
        // Do something

        // Success
        // Thread is finished
    }
});
stack.add(new Thread() {
    public void run()
    {
        // Do something

        // Success
        // Thread is finished
    }
});

for (int i=0; i<stack.size(); i++)
{
    stack.get(i).start();
}

I'd like to kill other thread when one has finished. How can I do that ? This is for an Android app.

Thanks.

ExecutorCompletionService在javadoc中完全具有此示例。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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