Code to simulate race condition in Java thread

后端 未结 4 2100
日久生厌
日久生厌 2021-01-05 07:15

I am new to Java multithreading. I am learning the concept of race condition.

Based on the Oracle document

http://docs.oracle.com/javase/tutorial/essential/

4条回答
  •  北荒
    北荒 (楼主)
    2021-01-05 07:22

    If the prints are shown "correctly", is because there are not much threads. Try to create 100 threads and you will see that prints are not shown sorted. If c in SynchronizedCounter is static, you can see race condition, since the threads are reading the same variable.

提交回复
热议问题