How to verify if command in the thread is successfully started in Java and another thread can be started?
I have several threads and one of them is running HTPP service using ProcessBuilder.
HttpThread class
ProcessBuilder pb = new ProcessBuilder(command);
Process process pb.start();
process.waitFor();
int exitValue = process.exitValue();
I would like to have another thread wait when the service is started. What is the best way to do it?
Sleep doesn't seem to me good solution. Perhaps would be possible to use s开发者_StackOverflow中文版ome method HttpThread.isReady(boolean b) but I couldn't setup this flag since after starting the command the service stays up and running and the thread is waiting when service will be finished.
Another way can be create one more thread and do socket connection but also not sure if it's good solution.
What do you think?
This sounds like an instance of the Producer/Consumer problem. Why don't you just use the wait/notify
mechanism?
Here's an academic example.
Your options are rather limited, since the HTTP service exists outside the JVM.
- You could poll the other process using inter-process communication, such as a socket.
- You could block until the process writes a ready message to
Process.getOutputStream()
.
精彩评论