* Note: this object knows nothing about Pipe,
* Note: this object knows nothing about Pipe, uses only a * generic WritableByteChannel. */ private static class Worker extends Thread { WritableByteChannel channel; private int reps; Worker (WritableByteChannel channel, int reps) { this.channel = channel; this.reps = reps; } // Thread execution begins here public void run() { ByteBuffer buffer = ByteBuffer.allocate (100); try { for (int i = 0; i < this.reps; i++) { doSomeWork (buffer); // channel may not take it all at once while (channel.write (buffer) > 0) { // empty } } this.channel.close(); } catch (Exception e) { // easy way out; this is demo code e.printStackTrace(); } } private String [] products = { “No good deed goes unpunished”, “To be, or what?”, “No matter where you go, there you are”, “Just say “Yo”", “My karma ran over my dogma” }; private Random rand = new Random(); private void doSomeWork (ByteBuffer buffer) { int product = rand.nextInt (products.length); buffer.clear(); buffer.put (products [product].getBytes()); buffer.put (”rn”.getBytes()); buffer.flip(); } } } 125
Hint: If you are looking for very good and affordable webspace to host and run your tomcat hosting application check Virtualwebstudio tomcat web hosting provider