This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java processes  RSS feed

 
Aaron Ravi Jakobovits
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the program I am writing, I must not use threads at all, I can only use processes. I am trying to understand where to perform operations on the data once a child process is created. Here is the code:

I want an arrayList in the parent process to search for a String in one half of an arrayList while the child process searches the other half of the arrayList for the same String, then communicate the count of the String back to the parent process. What I do not understand is WHERE to iterate through the arrayList in the child process. Once I send the data to the child through the stream, is there a way to do this inside the child process before sending the String count through to the parent, or am I already working on the child process when I use this line

and perform the search on arr2? I hope I am making sense. Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd think you're going to want to have separate classes to be your "parent" and "child" classes here. It sounds like you're imagining them both being the same, and although it's be possible to do it that way, you'd have to have a flag or some other way to distinguish between the two cases, and an "if" statement to choose between the behaviors.
 
Paul Clapham
Sheriff
Posts: 22491
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's your process as I understand it:

1. Your code builds a list.

2. It sends it to the "child".

3. The child (whose code you haven't shown) does something with the list.

4. Your code receives something from the child, presumably the result of step 3.

I'm a bit confused by your questions, but let's try this for answers:

1. You can do anything you like in the child process. I don't understand what you mean by "where to iterate through the list"; your child process would receive the list and then iterate through it.

2. Anything in that code which you posted there is not part of the child process. I have no idea what "working on the child process" means. In that line you posted, you are receiving data which was sent by the child process. Whatever that might be; you didn't say anything about the child process.
 
Aaron Ravi Jakobovits
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I must have missed something when I was trying to understand this, because I missed the fact that there are three methods provided by Java for a Process object, getErrorStream(), getInputStream(), and getOutputStream(). I think I understand what I need to do now. Ernest, do you think that creating separate classes with method calls would slow down the program if I am reading from a file?
 
Aaron Ravi Jakobovits
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run the program, this line causes my compiler to freeze:


I am flushing the stream; here is the full piece of code:

Any help is much appreciated.
 
Paul Clapham
Sheriff
Posts: 22491
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaron Ravi Jakobovits wrote:When I run the program, this line causes my compiler to freeze:


The compiler? Surely not. The compiler isn't involved when you run a Java program. It is only used to convert the source code into executable code.

Anyway you would be well advised to read When Runtime.exec() won't and to implement all of its suggestions.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since it's not clear we're really communicating well, let's do this a different way. Here is a tiny program which launches a copy of itself, sends a String to the copy via an ObjectOutputStream, and reads a reply back via an ObjectInputStream and prints it. This program works. Why not compile this, and then experiment with changing it to do whatever it is you're trying to do.

The only tricky part here is that if this program is started with no arguments, it becomes the parent, so it runs a second copy of itself, passing an argument, then initiates the data transfer. If the program is launched with an argument, it assumes it's the child.





 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!