1. import java.util.concurrent.atomic.AtomicInteger;
2. public class TestThread extends Thread {
3. private static final AtomicInteger count = new AtomicInteger();
4. public static void main(String[] args) {
5. while (true)
6. (new TestThread()).start();
7. }
8. @Override
9. public void run() {
10. System.out.println(count.incrementAndGet());
11. while (true)
12. try {
13. Thread.sleep(Integer.MAX_VALUE);
14. } catch (InterruptedException e) {
15. break;
16. }
17. }
18. }
http://developer.51cto.com/art/201111/304193.htm
2. public class TestThread extends Thread {
3. private static final AtomicInteger count = new AtomicInteger();
4. public static void main(String[] args) {
5. while (true)
6. (new TestThread()).start();
7. }
8. @Override
9. public void run() {
10. System.out.println(count.incrementAndGet());
11. while (true)
12. try {
13. Thread.sleep(Integer.MAX_VALUE);
14. } catch (InterruptedException e) {
15. break;
16. }
17. }
18. }
http://developer.51cto.com/art/201111/304193.htm
并发编程:利用AtomicInteger实现线程安全的计数器
本文介绍了如何使用Java的AtomicInteger类在多线程环境下实现一个线程安全的计数器。通过创建无限循环的线程并启动它们,演示了如何确保计数器的值在并发操作下仍然保持一致性和准确性。
86万+

被折叠的 条评论
为什么被折叠?



