• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

adding Process into an Array

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! I need to create an ArrayList<Process>. My problem is when adding a new Process in my array. I'm trying it with the next code line:



But the result of this is an empty array. I don't know how to create a new instance of Process, beacause it's an abstract class.
Could you give me a hand?

Thanks!
Hernán.
 
Hernan Blanco
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ouh, and something else:

Later I want to know if a process of my array wasn't executing any time or was executing and then destroyed. Can I use the next sentence?



My intention is enter into the if body only if the process isn't working. You think will it work? Or maybe I should use the exitValue() method...

Hernán.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hernan Blanco wrote: . . . an empty array.

What array? You haven't got any arrays.

I don't know how to create a new instance of Process . . ..

Yes, you do; you are doing it correctly, with the Runtime.exec() method.

But why do you want a List<Process> or a Process[] array? Remember that during their lifetime in that List or array, these Processes will be running, their output and error Streams must be continually "emptied", and their exitValues queried at the right time. The waitFor() method may cause problems for the current Java process if it has several native Processes to wait for.

Yes, you can use the == null test, but maybe != null would work better.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Yes, you can use the == null test, but maybe != null would work better.



My reading of the question was, whether, after adding a Process to the List, he could check if the process had completed by checking if the entry in the list was now null.
If I'm right, then the answer is no, because the Process instance will still exist even when the process it is running has completed.
 
Hernan Blanco
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:

Campbell Ritchie wrote:Yes, you can use the == null test, but maybe != null would work better.



My reading of the question was, whether, after adding a Process to the List, he could check if the process had completed by checking if the entry in the list was now null.
If I'm right, then the answer is no, because the Process instance will still exist even when the process it is running has completed.



Yes, I was thinking the same. For solve it, I can put also a new condition, with the method exitValue(), to take that into account.

Campbell Ritchie wrote:But why do you want a List<Process> or a Process[] array?



My target is opening several process (five blocks of four programs) simultaneously. For this, I have two ListArray<Process>(5) containing the subprocess of two kind of programs (I will call them "monitor" and "server"). So, each "block" of programs is a monitor, a server, and two clients (I'll call them "agent2d"). I don't need to keep the subprocess identifier of the agent2d programs. Beacause of this, I only need these two ListArray of monitors and servers.

I want to have always working five "blocks", so I'm paying attention when one of them finish, starting a new "block" when that happens. That's the reason cause I need to have a list of Process, and I search every time if any process finished.

If you are thinking another way to do this, I will hear you!

And my initial problem is still unsolved! when I run my program, the error message is: Exception: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hernan Blanco wrote:And my initial problem is still unsolved! when I run my program, the error message is: Exception: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0



Don't know what's causing that. I would need to see more of the code and the full stack trace.

Hernan Blanco wrote:If you are thinking another way to do this, I will hear you!



Personally I would create a wrapper class around the Process instance. This class could handle the monitoring of the stdout and stderr streams and also check for the process finishing (using the waitFor method).
I would also make this class generate an event when the process had completed. Your main class could then register itself as a listener for that event and so wouldn't have to continuously monitor all the Processes.
 
Hernan Blanco
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I know why I have this problem. It's beacause the MonProc.get(i) == null condition. I'm calling this method when I didn't add any element into my list. So, with i=4, the sentence is: (MonProc.get(4) == null), but in the list there isn't any element at position 4. How can I do to know if there's an element at the i position? I thougth it is with the get() method, but it give me an error.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hernan Blanco wrote:Now I know why I have this problem. It's beacause the MonProc.get(i) == null condition. I'm calling this method when I didn't add any element into my list. So, with i=4, the sentence is: (MonProc.get(4) == null), but in the list there isn't any element at position 4. How can I do to know if there's an element at the i position? I thougth it is with the get() method, but it give me an error.



MonProc.size() will tell you how many elements are in the list
 
Hernan Blanco
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mmm yes but...

I know the maximum size of my list: 5. But my doubt is, when I remove an element from the list, the position keeps empty, or the elements get reordered? I'll draw an example
(1 is used position, 0 is empty)

[1] [1] [1] [1] [1]

And I remove the third position... the result is [1] [1] [0] [1] [1] or [1] [1] [1] [1] [0], moving the greater elements to lower positions?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hernan Blanco wrote:I know the maximum size of my list: 5



5 is not the maximum size of your list - it is the initial capacity. There is nothing (other than the amount of memory you have) that will stop you adding more than 5 objects to the list.

Hernan Blanco wrote:But my doubt is, when I remove an element from the list, the position keeps empty, or the elements get reordered?



The elements get reordered and the size of your list decreases by 1.

Hernan Blanco wrote:And I remove the third position... the result is [1] [1] [0] [1] [1] or [1] [1] [1] [1] [0]



Neither. The result is [1] [1] [1] [1] and if you try to access the fifth element you will get an exception as you have already seen.

If you want to maintain a space for a process that you remove from the list and the maximum number of processes is known then you should use an array, not an ArrayList.
If the maximum number of processes is not known, then you need to use a Map.
 
Hernan Blanco
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joanne:
Yeah... It's my first experience with Java and I didn't understand very well the concept of ArrayList. I was trying to use it like a C array (I come from C and C++ xD). But I was complicating me needlessly, because a normal array is the best solution for my problem.


Now, I have another trouble. I want to close one of my process kept at my array. For this, I'm using the destroy() method. But actually I want to send a SIGTERM signal, not a SIGKILL signal (the program should close files and resources when I send the termination signal). I think that destroy() is sending a SIGKILL, that kills the process immediately, and doesn't allow the program to do the latest operations (specially writing an output file).

How can I do that?

PD: I'm working on Ubuntu :P

Hernán.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you find the code of the JVM and see how to override the destroy() method? I suspect you would be sacrificing platform-independence if you do that.

Anybody else got any ideas?
 
reply
    Bookmark Topic Watch Topic
  • New Topic