/**
*
* @author devin
* @date 2021-01-07 15:52
* @version 1.0.2
*/
public class ChanTest {
private static FileOutputStream fileOutputStream;
private static FileChannel fileChannel;
public static <buffer, buffer1, buffer2> void main(String[] args) throws IOException, InterruptedException {
fileOutputStream = new FileOutputStream("D://out.txt");
fileChannel = fileOutputStream.getChannel();
for(int i = 0;i < 10;i++){
Thread thread1 = new Thread(){
public void run() {
ByteBuffer buffer1 = ByteBuffer.wrap("123\r\n".getBytes());
try {
fileChannel.write(buffer1);
} catch (IOException e) {
e.printStackTrace();
}
}
};
Thread thread2 = new Thread(){
public void run() {
ByteBuffer buffer2 = ByteBuffer.wrap("654\r\n".getBytes());
try {
fileChannel.write(buffer2);
} catch (IOException e) {
e.printStackTrace();
}
}
};
thread1.start();
thread2.start();
}
Thread.sleep(3000);
fileChannel.close();
fileOutputStream.close();
}
}