且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Java中使用System.out.println的多个线程

更新时间:2023-02-09 14:06:28

这里可能是一些示例代码解决问题:

Here might be some sample code to fix the problem:

while(iterator.hasNext())
{
    SelectionKey key = iterator.next();

    channel.receive(buffer);     // The buffer is a ByteBuffer.
    buffer.flip();
    byte[] bytes = new byte[buffer.limit()];  // copy buffer contents to an array
    buffer.get(bytes);
    // thread will convert byte array to String
    new Thread(new ThreadToPrintTheMessage(bytes)).start();

    buffer.clear();

    iterator.remove();
}