is System.out.printf() statement asynchronous?

后端 未结 3 1402
名媛妹妹
名媛妹妹 2021-01-22 08:22

I am printing information to test Threads using Reentrant locks. I am creating fair locks using statement new ReentrantLock(true).

In one of the object method where I a

3条回答
  •  心在旅途
    2021-01-22 09:16

    System.out is a buffered stream. This mean that it doesn't fill the console synchroneously. While the printf() call is synchrone, the console output is not.

    You can either:

    • flush() System.out by calling System.out.flush();,
    • use println() which auto-flushes the stream,
    • use System.err which is not AFAIK buffered.

    Not sure about the last one...

    Note that your threads are executed concurrently (if you use .start()) Which means you cannot expect precisely any execution order without synchronization elements.

提交回复
热议问题