Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Firstly, the process will only be as fast as the slowest piece. If the timing breakdown is:</p> <ul> <li>gunzip: 1 seconds</li> <li>sed: 5 seconds</li> <li>gzip: 1 second</li> </ul> <p>by going multi-threaded you'll be done in <em>at best</em> 5 seconds instead of 7.</p> <p>Secondly, rather than using the queues you're using, instead try to replicate the functionality of what you're copying and use <a href="http://java.sun.com/javase/6/docs/api/java/io/PipedInputStream.html" rel="noreferrer"><code>PipedInputStream</code></a> and <a href="http://java.sun.com/javase/6/docs/api/java/io/PipedOutputStream.html" rel="noreferrer"><code>PipedOutputStream</code></a> to chain together processes.</p> <p><strong>Edit:</strong> there are a few ways of handling related tasks with Java concurrency utils. Divide it into threads. Firstly create a common base class:</p> <pre><code>public interface Worker { public run(InputStream in, OutputStream out); } </code></pre> <p>What this interface does is represent some arbitrary job that process input and generates output. Chain these together and you have a pipeline. You can abstract away the boilerplate too. For this we need a class:</p> <pre><code>public class UnitOfWork implements Runnable { private final InputStream in; private final OutputStream out; private final Worker worker; public UnitOfWork(InputStream in, OutputStream out, Worker worker) { if (in == null) { throw new NullPointerException("in is null"); } if (out == null) { throw new NullPointerException("out is null"); } if (worker == null) { throw new NullPointerException("worker is null"); } this.in = in; this.out = out; this.worker = worker; } public final void run() { worker.run(in, out); } } </code></pre> <p>So, for example, the <code>Unzip</code> PART:</p> <pre><code>public class Unzip implements Worker { protected void run(InputStream in, OutputStream out) { ... } } </code></pre> <p>and so on for <code>Sed</code> and <code>Zip</code>. What then binds it together is this:</p> <pre><code>public static void pipe(InputStream in, OutputStream out, Worker... workers) { if (workers.length == 0) { throw new IllegalArgumentException("no workers"); } OutputStream last = null; List&lt;UnitOfWork&gt; work = new ArrayList&lt;UnitOfWork&gt;(workers.length); PipedOutputStream last = null; for (int i=0; i&lt;workers.length-2; i++) { PipedOutputStream out = new PipedOutputStream(); work.add(new UnitOfWork( last == null ? in, new PipedInputStream(last), out, workers[i]); last = out; } work.add(new UnitOfWork(new PipedInputStream(last), out, workers[workers.length-1); ExecutorService exec = Executors.newFixedThreadPool(work.size()); for (UnitOfWork w : work) { exec.submit(w); } exec.shutdown(); try { exec.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedExxception e) { // do whatever } } </code></pre> <p>I'm not sure you can do much better than that and there is minimal code to write for each job. Then your code becomes:</p> <pre><code>public static processFile(String inputName, String outputName) { pipe(new FileInputStream(inputFile), new FileOutputStream(outputFile), new Zip(), new Sed(), new Unzip()); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload