How do threads that rely on one another communicate in Java?
For example, I am building a web crawler with threads that need data that comes from other threads.
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.
That depends on the nature of the communication.
and so on.
The simplest and most advisable form of inter-thread communication is simply to wait for the completion of other threads. That’s most easily done by using
Future:The second task won’t execute until the first completes.
Java 5+ has many concurrent utilities for dealing with this kind of thing. This could mean using
LinkedBlockingQueues,CountDownLatchor many, many others.For an in-depth examination of concurrency Java Concurrency in Practice is a must-read.