zhugw (1) [Avatar] Offline
#1
when I run lisitng 2-9 in eclipse, there is nothing output. Then I run it in doc environment, nothing output too. I don't know why is this? Anybody have found it too or know its reason?
My code in below:
Path largetxt = Paths.get("c:\test\large.txt");
try {
AsynchronousFileChannel channel = AsynchronousFileChannel
.open(largetxt);
ByteBuffer buffer = ByteBuffer.allocate(100_001);
channel.read(buffer, 0, buffer,
new CompletionHandler<Integer, ByteBuffer>() {
@Override
public void completed(Integer result,
ByteBuffer attachment) {
System.out.println("Bytes read[" + result + "]");
}
@Override
public void failed(Throwable exc, ByteBuffer attachment) {
System.out.println(exc.getMessage());
}
});
} catch (IOException e) {
e.printStackTrace();
}

Message was edited by:
zhugw
martijn.verburg (86) [Avatar] Offline
#2
Re: about listing 2-9, why no output?
Do you have a file at the location C: estlarge.txt ?
mhaligowski (2) [Avatar] Offline
#3
Re: about listing 2-9, why no output?
I see that original author is not responding, but I just have run into the same problem.

The file is certainly there (copied the path from fine-working Future example), there is absolutely no output.

Interestingly, when I add an inifinte loop at the end it works. It seems to me that on my machine the reading thread does not finish before the main thread, and so the program exits before the reading is done.

In the futures example it is done with while (!result.isDone()) {} loop. Any idea how one can solve it in Callbacks?
mhaligowski (2) [Avatar] Offline
#4
Re: about listing 2-9, why no output?
OK, I got this. How about that: http://pastebin.com/mMXSFLN8.

It's working fine now. It surely could be done better, but I haven't come to the concurrency chapter yetsmilie