I want to start several callable threads in the same time(in loop), and want to return some information from everyone in main thread. How to realise this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s impossible to say the best approach from your question, because we know nothing about the use case! But generally speaking you could create a thread pool using
Executors.newFixedThreadPool(), then submit genericCallable<T>objects to the thread pool – you’ll get back aFuture<T>.In the main thread, you can then loop over the futures, calling each one of the
get()methods which will block until the corresponding callable has finished execution in the thread pool.