Multiple threads using System.out.println in Java

别来无恙 提交于 2020-01-10 19:48:05

问题


I have a multi-threaded Java application that will output information about a message it receives to the console for debugging purposes. Each time the application receives a message, it will call a System.out.println(String) on the message.

The problem that I am having is that if the application gets flooded with messages, System.out.println() prints erroneous information (like old buffer information). This is leading me to wonder if there is a threading issue where multiple threads are calling the println function at one time, and not properly flushing the buffer.

In my main program (thread), I have something to the effect of:

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

    channel.receive(buffer);     // The buffer is a ByteBuffer.
    buffer.flip();

    new Thread(new ThreadToPrintTheMessage(buffer)).start();

    buffer.clear();

    iterator.remove();
}

In my thread, I have something to the effect of:

@Override
public void run()
{
    System.out.println(message);
    System.out.flush();   // I have better results with this.  But, it doesn't
                          // fully resolve the issue.
}

Is there a simple way for me to have multiple threads print out to the console at one time without the buffers containing old information?

Thanks

EDIT: updated the code in the main thread to be more representative of what my program is doing.


回答1:


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();
}



回答2:


synchronized (System.out) {
    System.out.println(message);
    System.out.flush();
}



回答3:


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.




回答4:


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");


来源:https://stackoverflow.com/questions/9467759/multiple-threads-using-system-out-println-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!