Multiple threads using System.out.println in Java

徘徊边缘 提交于 2019-11-30 15:11:07

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();
}
synchronized (System.out) {
    System.out.println(message);
    System.out.flush();
}

I haven't got time to check println source be be sure it's thread safe always (you could, if you wanted to), but are you sure that your println is wrong? It may well be that code is running at a very different times than you think. Threads often get hung up on locks or just get forgotten by the scheduler, so what you think should run A, B, C, D can run B, C, D, A. Then you wonder why println is messed up, printing last what you think ran first. And this is a really simple example. The difference between what you expect to happen with multi-threading and what does happen can be truly astounding. Generally, the higher the thread-to-core ratio, the worse it is. Single core machines always do everything the opposite of what you would expect.

And you don't even need multiple threads to have this problem. My first jolts came with event queues (on Windows 3.1), which did not share my views on when things should run. It took me a while to figure out that the messages were scrambled because the OS had a much different idea of how they should run than I did.

There may well be some subtleties with System.out.println and flush that I don't know about, but even when you get all that working, be aware that those threads have rather contrary minds of their own. Even Logger won't solve all your problems.

You should be using Java.util.logging or some other logging framework for this purpose.

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/logging/Logger.html

import java.util.logging.Logger;
....
Logger log = Logger.getLogger("com.something.something");
....
log.log(Level.Info, "Message");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!