Created
September 6, 2014 07:14
-
-
Save masahitojp/8d183478eaadfd50bf0c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.masahito; | |
import java.io.IOException; | |
import java.nio.ByteBuffer; | |
import java.nio.channels.AsynchronousFileChannel; | |
import java.nio.channels.CompletionHandler; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.nio.file.StandardOpenOption; | |
import java.util.concurrent.CountDownLatch; | |
public class AsyncFileWriteMain { | |
private static final int LINE_SIZE = 10; | |
private static final CountDownLatch responseWaiter = new CountDownLatch(10); | |
public static void main(String[] args) throws IOException { | |
final Path path = Paths | |
.get("path_to_file"); | |
try (final AsynchronousFileChannel channel = AsynchronousFileChannel.open( | |
path, StandardOpenOption.WRITE, StandardOpenOption.CREATE)) { | |
System.out.println("Start to write"); | |
final CompletionHandler<Integer, String> handler = new CompletionHandlerImpl(); | |
for (int i = 0, position = 0; i < LINE_SIZE; i++) { | |
final String message = "Hello World-" + Integer.toString(i) + "\n"; | |
// Buffer、書き込み位置、アタッチメント、ハンドラを与えて書き込み処理実行 | |
channel.write(ByteBuffer.wrap(message.getBytes()), position, getAttachment(i), handler); | |
// 次にメッセージを書き込む位置を設定 | |
position += message.length(); | |
} | |
responseWaiter.await(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
/* | |
* Attachmentはサンプルなので適当なメッセージを返す。 | |
*/ | |
private static String getAttachment(int i) { | |
return (i + 1) == 1 ? Integer.toString(i + 1) + " time" : Integer.toString(i + 1) + " times"; | |
} | |
private static final class CompletionHandlerImpl implements CompletionHandler<Integer, String> { | |
@Override | |
public void completed(Integer result, String attachment) { | |
System.out.println("[Completed to write] Current thread : " + Thread.currentThread().getId() + " Attachment : " + attachment + " Written bytes : " + result); | |
responseWaiter.countDown(); | |
} | |
@Override | |
public void failed(Throwable e, String attachment) { | |
System.out.println("[Failed to write] Current thread : " + Thread.currentThread().getId() + " Attachment : " + attachment + " Exception : " + e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment