miercuri, 15 februarie 2012

Java: from finite to infinite

In Mathematics there are properties/relations that holds in finite cases, but stops to work in infinite cases.
In common life one can buy a printer made in China and print 5-10 copies per day and the printer will wok to the end of his/her life, but if he/she wil try to print more than 100 copies every day the printer will broke soon.
Mathematics, ordinary life ...? Let's talk about Java!

Case 1. Reading files.
for(File file : fileList) {
  LineIterator it = FileUtils.lineIterator(file);
  while (it.hasNext()) {
     String line = it.nextLine();
     // do something with line
  }
}

For a small number of files the above code will work like a charm, but for a very large fileList, after some time and in some circumstances (depending on CPU, RAM, version of Java etc) we will get IOException: Too many open files.

Case 2. Executing commands.

for(String command : commandList) {
  Process p = Runtime.getRuntime().exec(command, null);
  p.waitFor();
}

The result will be the same as in Case 1 with the same IOException: Too many open files.

Case 3. Requesting URLs

... coming soon...

Solutions.

The exceptions from Case 1  and Case 2 may be avoided with a simple finally block where we must dispose/close some of the used objects.
Here we have the solution for the Case 1

for(File file : fileList) {
  LineIterator it = FileUtils.lineIterator(file, "UTF-8");
  try {
    while (it.hasNext()) {
      String line = it.nextLine();
      // do something with line
    }
  } finally {
    it.close();
  }
}
And in Remember to Close Your Streams When Using Java's Runtime.getRuntime().exec() we have the solution for the Case 2.
for(String command : commandList) {
  Process p = null;
  try {
    p = Runtime.getRuntime().exec(command, null);
    p.waitFor();
  } finally {
    if(p != null) {
      IOUtils.closeQuietly(p.getOutputStream());
      IOUtils.closeQuietly(p.getErrorStream());
      IOUtils.closeQuietly(p.getInputStream());
  }
}

Niciun comentariu:

Trimiteți un comentariu