Example 2

Compress the files f1 and f2 in the directory d to f1.bz2 and f2.bz2 in the same directory. Use an bzip2 encoder executor service to spread the encoding over as many threads as there are available CPU:s.

Groovy

// Enable the task package in the script header // enableTaskPackage org.at4j import org.at4j.comp.bzip2.BZip2OutputStream import org.entityfs.util.Directories import org.schmant.run.TaskExecutor import org.schmant.support.FutureFile import org.schmant.task.at4j.bzip2.BZip2TF // Create the executor service. There are two static methods on // BZip2OutputStream for this. The one without arguments creates an executor // service that will use one thread for each available CPU. def executorService = BZip2OutputStream.createExecutorService() try { // Use a task executor to run the two bzip2 tasks in parallel. Otherwise they // would not be able to share the threads of the executor service. Using an // executor service would still give a performance improvement for each task, // though, even if they were not run in parallel. def te = new TaskExecutor(). setNumberOfThreads(2). start() try { // Don't set a target file. The default is to create a target file with the // source file name and the extension .bz2 te.add(new BZip2TF(). setSource(Directories.getFile(d, "f1")). // Delete the source file setDeleteSourceFile(true). setExecutorService(executorService)) te.add(new BZip2TF(). setSource(Directories.getFile(d, "f2")). // Delete the source file setDeleteSourceFile(true). setExecutorService(executorService)) te.waitFor() } finally { // Shut down the task executor. // This must be shut down BEFORE the executor service to ensure that the // bzip2 tasks have actually had a chance to encode their data before the // executor service is shut down. te.shutdown() } } finally { // Shut down the executor service to release its resources. executorService.shutdown() }

JavaScript

enableTaskPackage("org.at4j"); // Create the executor service. There are two static methods on // BZip2OutputStream for this. The one without arguments creates an executor // service that will use one thread for each available CPU. var executorService = BZip2OutputStream.createExecutorService(); try { // Use a task executor to run the two bzip2 tasks in parallel. Otherwise they // would not be able to share the threads of the executor service. Using an // executor service would still give a performance improvement for each task, // though, even if they were not run in parallel. var te = new TaskExecutor(). setNumberOfThreads(2). start(); try { // Don't set a target file. The default is to create a target file with the // source file name and the extension .bz2 te.add(new BZip2TF(). setSource(Directories.getFile(d, "f1")). // Delete the source file setDeleteSourceFile(true). setExecutorService(executorService)); te.add(new BZip2TF(). setSource(Directories.getFile(d, "f2")). // Delete the source file setDeleteSourceFile(true). setExecutorService(executorService)); te.waitFor(); } finally { // Shut down the task executor. // This must be shut down BEFORE the executor service to ensure that the // bzip2 tasks have actually had a chance to encode their data before the // executor service is shut down. te.shutdown(); } } finally { // Shut down the executor service to release its resources. executorService.shutdown(); }

JRuby

enableTaskPackage("org.at4j") # Create the executor service. There are two static methods on # BZip2OutputStream for this. The one without arguments creates an executor # service that will use one thread for each available CPU. executorService = Schmant::BZip2OutputStream.createExecutorService begin # Use a task executor to run the two bzip2 tasks in parallel. Otherwise they # would not be able to share the threads of the executor service. Using an # executor service would still give a performance improvement for each task, # though, even if they were not run in parallel. te = Schmant::TaskExecutor.new. setNumberOfThreads(2). start; begin # Don't set a target file. The default is to create a target file with the # source file name and the extension .bz2 # Delete the source file te.add(Schmant::BZip2TF.new. setSource(Schmant::Directories.getFile($d, "f1")). setDeleteSourceFile(true). setExecutorService(executorService)) te.add(Schmant::BZip2TF.new. setSource(Schmant::Directories.getFile($d, "f2")). setDeleteSourceFile(true). setExecutorService(executorService)) te.waitFor ensure # Shut down the task executor. # This must be shut down BEFORE the executor service to ensure that the # bzip2 tasks have actually had a chance to encode their data before the # executor service is shut down. te.shutdown end ensure # Shut down the executor service to release its resources. executorService.shutdown end

Jython

enableTaskPackage("org.at4j") # Create the executor service. There are two static methods on # BZip2OutputStream for this. The one without arguments creates an executor # service that will use one thread for each available CPU. executorService = BZip2OutputStream.createExecutorService(); try: # Use a task executor to run the two bzip2 tasks in parallel. Otherwise they # would not be able to share the threads of the executor service. Using an # executor service would still give a performance improvement for each task, # though, even if they were not run in parallel. te = TaskExecutor(). \ setNumberOfThreads(2). \ start() try: # Don't set a target file. The default is to create a target file with the # source file name and the extension .bz2 # Delete the source file te.add(BZip2TF(). \ setSource(Directories.getFile(d, "f1")). \ setDeleteSourceFile(True). \ setExecutorService(executorService)) te.add(BZip2TF(). \ setSource(Directories.getFile(d, "f2")). \ setDeleteSourceFile(True). \ setExecutorService(executorService)) te.waitFor() finally: # Shut down the task executor. # This must be shut down BEFORE the executor service to ensure that the # bzip2 tasks have actually had a chance to encode their data before the # executor service is shut down. te.shutdown() finally: # Shut down the executor service to release its resources. executorService.shutdown()


* An EntityFS-aware task is implemented using EntityFS. This means that it uses the filter settings of DirectoryView:s and also that it often can work with other file system implementations than File-based, such as the RAM file system.