@@ -0,0 +1,2294 @@
+package com.github.zhongl.ex.nio;%0A%0Aimport com.github.zhongl.util.Benchmarks;%0Aimport com.github.zhongl.util.FileTestContext;%0Aimport com.google.common.io.Closeables;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0A%0Aimport java.io.File;%0Aimport java.io.IOException;%0Aimport java.nio.ByteBuffer;%0Aimport java.nio.channels.FileChannel;%0Aimport java.util.concurrent.CountDownLatch;%0A%0A/** @author %3Ca href=%22mailto:zhong.lunfu@gmail.com%22%3Ezhongl%3Ca%3E */%0Apublic class ThreadAndChannelWritingBenchmark extends FileTestContext %7B%0A%0A    public static final int SIZE = 64;%0A    private Runnable writing;%0A%0A    @Override%0A    @Before%0A    public void setUp() throws Exception %7B%0A        super.setUp();%0A        dir = testDir(%220%22);%0A        writing = new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                long l = System.nanoTime();%0A                FileChannel channel = FileChannels.getOrOpen(new File(dir, l + %22%22));%0A                try %7B%0A                    for (int i = 0; i %3C SIZE; i++) %7B%0A                        channel.write(ByteBuffer.allocate(1024 * 1024));%0A                    %7D%0A                    channel.force(false);%0A                %7D catch (IOException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A                Closeables.closeQuietly(channel);%0A            %7D%0A        %7D;%0A    %7D%0A%0A    @Test%0A    public void singleThreadWrite() throws Exception %7B%0A        Benchmarks.benchmark(%22single thread: %22, writing, 1);%0A    %7D%0A%0A    @Test%0A    public void multipThreadsWrite() throws Exception %7B%0A        final int num = Runtime.getRuntime().availableProcessors() * 2;%0A        final CountDownLatch latch = new CountDownLatch(num);%0A%0A        final Runnable wrap = new Runnable() %7B%0A%0A            @Override%0A            public void run() %7B%0A                writing.run();%0A                latch.countDown();%0A            %7D%0A        %7D;%0A%0A        Benchmarks.benchmark(%22multip threads: %22, new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                for (int i = 0; i %3C num; i++) %7B%0A                    new Thread(wrap).start();%0A                %7D%0A                try %7B%0A                    latch.await();%0A                %7D catch (InterruptedException e) %7B%0A                    e.printStackTrace();  // TODO right%0A                %7D%0A            %7D%0A        %7D, num);%0A    %7D%0A%7D%0A

Introduce ThreadAndChannelWritingBenchmark.