Nonsense opening
Java programs are better, But I think Java programming has never been able to say that I am proficient in Java programming for more than ten years. Due to work reasons, I started to fight Java multithreading, I have never been touched before, so I want to keep some footprints on these two roads.
Start with the topic
It's easy to read an article about whether the method is thread-safe. However, as I was just getting started, I took the first step. Because the comments are still relatively detailed, so we will not talk nonsense, directly go to the code.
This method is not a thread-safe NotThreadSafeCounter counter = 0 System. out. println ("counter:" + Thread. sleep (1500} counter ++ (I = 0; I <5; I ++} View Code this method is thread-safe (synchronized) counter = 0 System. out. println ("counter:" + counter ++ (I = 0; I <5; I ++} View Code this method is also thread-safe (AtomicInteger) atomicInteger counter = AtomicInteger (0 System. out. println ("counter:" + (I = 0; I <5; I ++} View Code time tail
I just got started. If you have any questions, please give me more advice and exchange.